Enum with default type? is it possible?

Is it possible to make a typical conversion type for an enumeration?

I use an enumeration for a large number, for example, state, and I want to compare the enumerations directly with LINQ fields, but I have to output all the time.

+3
source share
4 answers

The answer was much simpler!

My good friend told me that it is very easy! take a look at this sample!

public enum State:byte
{
    EmailNotValidated = 0x00,
    EmailValidated = 0x10,
    Admin_AcceptPending = 0x40,
    Active = 0x80,
    Admin_BlockedAccount = 0xff
}

Pay attention to the part : BYTE after the name Enum ... there is a trick I was looking for! But thanks to everyone who is trying for me!

+2
source

LINQ, . , .

, , , . LINQtoSQL. , "" Visual Studio.

+2

LINQ-to-SQL () (: ). : enum -, : Some.Namespace.MyEnum. .

(, varchar , "" [ ]), int/varchar ( ..) . , private FooStorage :

partial class MyType {
    public MyEnum Foo {
        get {... mapping logic reading from FooStorage...}
        set {... mapping logic, updating FooStorage...}
    }
}

, LINQ ( ).

+1

?

public enum MyEnum
{
    First = 1,
    Second = 2,
    Third = 3
}

public static class Utility
{
    public static string Description(this Enum e)
    {
        Type t = e.GetType();
        DescriptionAttribute[] desc =
            (DescriptionAttribute[])(t.GetField(e.ToString())
            .GetCustomAttributes(typeof(DescriptionAttribute), false));
        return desc.Length > 0 ? desc[0].Description : e.ToString();
    }
    public static byte ToByte(this Enum ai)
    {
        object o=Enum.ToObject(ai.GetType(), ai);
        return Convert.ToByte(o);
    }
}


class Program
{
    static void Main(string[] args)
    {
        MyEnum me = MyEnum.Third;

        Console.WriteLine("Value: {0}\r\nType: {1}"
        ,me.ToByte(),me.ToByte().GetType().ToString());

        Console.ReadLine();
    }
}

:

: 3

: System.Byte

0

Source: https://habr.com/ru/post/1708543/


All Articles