Reputation: 6083
hi all as i know that we can change value type in referance type by boxing and unboxing concept. so i want to ask that enum is a value type so can we change it in referance type ? like int a =5 is a value type we can convert it as referance type ,can we do same thing with enum;
Upvotes: 0
Views: 102
Reputation: 9469
Yes it is possible to box an enum because the underlying value is a numeric value. You may also change its underlying type by using the following syntax:
public enum Test : byte
{ /* Values */ }
Please note that the byte type is useful in case you want to use the enum values as flags
Upvotes: 0
Reputation: 1500535
Your question is somewhat unclear, but yes, you can box enum values in the same way as other value types. One interesting point is that you although the boxed enum value retains the appropriate enum type, you can unbox from an enum to its underlying type, or vice versa:
using System;
enum Color { Red, Green, Blue }
class Test
{
static void Main()
{
Color c = Color.Blue;
object o = c;
int i = (int) o;
Console.WriteLine(i); // Prints 2
i = 1;
o = i;
c = (Color) o;
Console.WriteLine(c); // Prints Green
}
}
Upvotes: 3