Enumeration is a value type, while System. Enum is a reference type.

Source: Internet
Author: User
Tags mscorlib

Q: In C #, how do we express enumeration types?

A: You can use the enum keyword (keyword) to declare an enumeration type (enum type ):

 
 
  1. // Code #01  
  2. public enum Alignment  
  3. {  
  4.     Left,  
  5.     Center,  
  6.     Right  
  7. }  

--------------------------------------------------------------------------------
Q: C # Is the enumerated type value type or reference type )?

A: All enumeration types are value types.

--------------------------------------------------------------------------------

Q: Is System. Enum an enumeration type?

A: No.

--------------------------------------------------------------------------------

Q: What is the relationship between System. Enum and enum type?

A: System. Enum is an abstract class. All enumeration types are directly inherited from it, and of course all its members are inherited.

--------------------------------------------------------------------------------

Q: Why does System. Enum belong to the reference type?

A: Yes.

--------------------------------------------------------------------------------

Q: Since System. Enum is a reference type and the enumeration type is directly inherited from System. Enum, why is the enumeration type not a reference type?

A: This inheritance relationship is implicitly expanded by the compiler. The above Code #1's Alignment enumeration is expanded with the following IL Code:

 
 
  1. // Code #02  
  2. .class public auto ansi sealed Aligment  
  3.        extends [mscorlib]System.Enum  
  4. {  
  5.     .field public static literal Aligment Left = int32(0x00000000)  
  6.     .field public static literal Aligment Center = int32(0x00000001)  
  7.     .field public static literal Aligment Right = int32(0x00000002)  
  8.  
  9.     .field public specialname rtspecialname int32 value__  
  10. }  

From the declaration, you can see that Aligment indeed inherits from System. Enum, but you cannot explicitly declare this inheritance relationship in C.

--------------------------------------------------------------------------------

Q: Why is the enumerated type still a value type after it is inherited from a reference type!

A: You know, all value types are System. the descendant of ValueType, And the enumeration type is no exception. The Enumeration type is directly inherited from System. enum, while System. the Enum is directly inherited from the System. valueType, so the enumeration type is also System. the descendant of ValueType.

--------------------------------------------------------------------------------

Q: slow! Shouldn't all types derived from System. ValueType be value types? Why is System. Enum a reference type?

A: The correct statement is that "All values are descendants of System. ValueType", but the descendants of System. ValueType are not all values. System. Enum is the only special case! Among all the descendants of System. ValueType, all except System. Enum are value types. In fact, we can find the declaration of System. Enum in the source code of. NET:

 
 
  1. public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible  

Note that the Enum declaration in. NET Framework SDK v2.0.3600.0 Documentation is incorrect:

Public abstract struct Enum: IComparable, IFormattable, IConvertible

--------------------------------------------------------------------------------

Q: I'm dizzy. What is the relationship between the C # Enumeration type, System. Enum, System. ValueType, value type, and reference type?

A: in short,

1. All enumeration types (enum type) are value types.
2. System. Enum and System. ValueType are reference types.
3. The enum type is implicitly directly inherited from System. Enum, and the inheritance relationship can only be automatically expanded by the compiler. However, System. Enum is not an enumeration type ).
4. System. Enum is a special case. It directly inherits from System. ValueType (see Code #03), but it is a reference type.
Okay. Now let's take a look at the following code. Can you guess the output result?

 
 
  1. // Code #04  
  2. static void Main()  
  3. {  
  4.     Type t = typeof(System.Enum);  
  5.  
  6.     if (t.IsEnum)  
  7.         Console.WriteLine("I'm enum type.");  
  8.  
  9.     if (t.IsValueType)  
  10.         Console.WriteLine("I'm value type.");  
  11. }  

Don't be surprised that the running result of the program has no output! For the first judgment, we know that System. Enum is not an enumeration type. But what about the second judgment? System. Enum is inherited from System. ValueType, but it is not recognized as a descendant of System. ValueType! This is a special case in. NET. It just shows that System. Enum is special.

--------------------------------------------------------------------------------

Q: Since the enumeration type is a value type, it will naturally involve the boxing and unboxing problems, what will the enumeration type be packed? [Updated]

A: enumeration types can be boxed into System. Enum, System. ValueType, System. Object, System. IConvertible, System. IFormattable, and System. IComparable.

Note: In.. NET 1.1, the enumeration type can only be boxed to System. enum, System. valueType, System. object.. NET 2.0, the enumeration type can also be boxed to System. the three interfaces implemented by Enum: System. IConvertible, System. IComparable, System. IFormattable. The corresponding packing operation can be either implicit or explicit.

The following C # code:

 
 
  1. // Code #05  
  2. // See Code #01 for Alignment.  
  3. static void Main()  
  4. {  
  5.     Alignment a = Alignment.Center;  
  6.  
  7.     Console.WriteLine(a.ToString());  
  8.  
  9.     Console.WriteLine(a);  
  10. }  

The corresponding IL code is:

 
 
  1. // Code #06  
  2. .method private hidebysig static void Main() cil managed  
  3. {  
  4.     .entrypoint  
  5.     // Code Size: 32 byte(s)  
  6.     .maxstack 1  
  7.     .locals (  
  8.           EnumerationFaq.Alignment alignment1)  
  9.     L_0000: ldc.i4.1   
  10.     L_0001: stloc.0   
  11.     L_0002: ldloc.0   
  12.     L_0003: box EnumerationFaq.Alignment  
  13.     L_0008: call instance string [mscorlib]System.Enum::ToString()  
  14.     L_000d: call void [mscorlib]System.Console::WriteLine(string)  
  15.     L_0012: nop   
  16.     L_0013: ldloc.0   
  17.     L_0014: box EnumerationFaq.Alignment  
  18.     L_0019: call void [mscorlib]System.Console::WriteLine(object)  
  19.     L_001e: nop   
  20.     L_001f: ret   
  21. }  

From the IL code, we can see that the enumeration type is boxed twice. The first time (L_0003) is packed into System. Enum, and the second time (L_0014) is packed into System. Object.

But if you ask the compiler to automatically select the packing type for you, it will give priority to System. Enum:

 
 
  1. // Code #07  
  2. // See Code #01 for Alignment.  
  3. class Program  
  4. {  
  5.     static void Main()  
  6.     {  
  7.         Alignment a = Alignment.Center;  
  8.  
  9.         Print(a);  
  10.     }  
  11.  
  12.     static void Print(IConvertible c)  
  13.     {  
  14.         Console.WriteLine(c);  
  15.     }  
  16.  
  17.     static void Print(IFormattable f)  
  18.     {  
  19.         Console.WriteLine(f);  
  20.     }  
  21.  
  22.     static void Print(IComparable c)  
  23.     {  
  24.         Console.WriteLine(c);  
  25.     }  
  26.  
  27.     static void Print(Object o)  
  28.     {  
  29.         Console.WriteLine(o);  
  30.     }  
  31.  
  32.     static void Print(ValueType v)  
  33.     {  
  34.         Console.WriteLine(v);  
  35.     }  
  36.  
  37.     static void Print(Enum e)  
  38.     {  
  39.         Console.WriteLine(e);  
  40.     }  
  41. }  

The above code will be compiled into the following IL:

  1. // Code #08
  2. . MethodPrivateHidebysigStatic VoidMain (String[] Args) Pencil managed
  3. {
  4. . Entrypoint
  5. // Code Size: 15 bytes (s)
  6. . Maxstack 1
  7. . Locals (
  8. EnumerationFaq. Alignment alignment1)
  9. L_0000: ldc. i4.1
  10. L_0001: stloc.0
  11. L_0002: ldloc.0
  12. L_0003: box EnumerationFaq. Alignment
  13. // Call static void Print (Enum e );
  14. L_0008: callVoidEnumerationFaq. Program: Print ([mscorlib] System. Enum)
  15. L_000d: nop
  16. L_000e: ret
  17. }

Statement: This article Reprinted from http://developer.51cto.com/art/200908/143309.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.