C # sharp experience fourteenth lectures

xiaoxiao2021-03-06  69

Fourteenth lecture

Enumeration Type is another lightweight value type in C #, C # uses a set of specific values ​​to express a set of specific values, such as Windows Forms, and the style of the button control. The following pseudo-code shows a typical application enumeration usage: public enum WritingStyle {Classical, Modern, Elegant,} class Essay {public void Write (WritingStyle writingStyle) {switch (writingStyle) {case WritingStyle.Classical: // Writing Classical Break; Case Writingstyle.modern: // Modern Writing Style Break; Case Writingstyle.egant: // Elegant Writing Style Break; Default: Throw (New System.ArgumentException ("Invalid Writing Style");}}} Note The above enumeration symbol Classical, Modern, Elegant is separated by a comma "," rather than the semicolon ";" The last enumeration value Elegant can be saved. Like the structure, the enumeration in C # does not allow it to have its own inheritance of the parent class system.enum, the same, enumeration can't be inherited, no Abstract. The System.enum class provides a lot of easy-to-use features for enumeration types. For example, we can get the string symbolic representation of our declaration values ​​via the getName method. The following example shows some commonly used operations: use system; public enum writestyle {classical, modern, elegant,} class test {public static void

Main

() {WritingStyle dw = WritingStyle.Modern; Console.WriteLine (Enum.GetName (typeof (WritingStyle), dw)); Console.WriteLine (dw.ToString ()); Console.WriteLine (Enum.GetUnderlyingType (typeof (WritingStyle) ))));}} The last line outputs SYSTEM.INT32, what is going on? We know that int is a short written form of System.Int32. Is our WritingStyle enumeration type and integer int type? Yes, the enumeration and integer values ​​of C # are strictly distinguished, for example, we cannot be assigned to DW = 1 in the above code. However, each enumeration value does have a value of an integer type, and can be converted, but this conversion must be expressed in a clear transformation syntax. We know that the integer type in C # has eight kinds of Byte, Sbyte, Short, Ushort, Int, uint, long, ulong, then which integer type corresponds to the enumeration value of the C #? It is related to the number of enumerated values ​​that can be accommodated. In fact, the C # enumeration type supports any of these eight integer types. According to our needs, you can specify when declaring the enumeration type, such as "Public Enum WritingStyle: Byte" specified its enumeration value. Integer type is Byte. This type can be obtained by the Enum.GetunderlyingType method described above. If not clearly specified, C # will use the int type as the type of enumeration value by default. Since the enumeration value is actually an integer value, then it's size? The reader will see the result in the main function of the following code. "Object Obj IN Enum.GetValues) {console.writeline (Obj.to) ": (int) obj);} The above code will produce the following output: Classical: 0Modern: 1Elegant : 2 You can see that C # defaults from 0 to our enumeration value to our enumeration value. Of course, we can also specify a value for enumeration values: public enum WritingStyle {classical = 0, modern = 10, elegant = 100,} We can even like the following, the premise is that we must ensure that their respective Numerical value. Public Enum WritingStyle {Classical = 0, MODERN = 10 Classical, Elegant = 10 MODERN,} Enumeration Value also has operational behavior such as integer, logic, arithmetic operation behavior, see below: use system; public enum WritingStyle {Classical = 0, MODERN = 10 Classical, Elegant = 10 MODERN,} Class test {public static voidmain

转载请注明原文地址:https://www.9cbs.com/read-119993.html

New Post(0)