Enum is Reference Type or Value Type?

前端 未结 4 1589
刺人心
刺人心 2020-12-02 16:36

I used Enum property in my EntityFramework 5 class, but in the database this field is nullable. Visual studio gives the error that this property must be a nullable property.

相关标签:
4条回答
  • 2020-12-02 17:12
    public enum TestReferenceOrValue
    {
        one, two, three    
    }
    var a = TestReferenceOrValue.one;
    var b = a;
    b = TestReferenceOrValue.three;
    

    If enums are by reference, changing b affects a
    Console.Write(a); → one
    Console.Write(b); → three

    a great online tool for cSharp => http://csharppad.com/

    0 讨论(0)
  • 2020-12-02 17:13

    suppose we have enum

    public enum eCategory
    {
        health ,        
        Weapon
    }
    

    and a type of eCategory such as :-

    eCategory currentcategory;
    

    then currentcategory is of value type

    0 讨论(0)
  • 2020-12-02 17:16

    If you do myEnum.SomeValue it will be a value type.

    0 讨论(0)
  • System.Enum is a reference type, but any specific enum type is a value type. In the same way, System.ValueType is a reference type, but all types inheriting from it (other than System.Enum) are value types.

    So if you have an enum Foo and you want a nullable property, you need the property type to be Foo?.

    0 讨论(0)
提交回复
热议问题