Get int value from enum in C#

前端 未结 28 1959
臣服心动
臣服心动 2020-11-22 04:58

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this.

public e         


        
28条回答
  •  鱼传尺愫
    2020-11-22 05:37

    The easiest solution I can think of is overloading the Get(int) method like this:

    [modifiers] Questions Get(Question q)
    {
        return Get((int)q);
    }
    

    where [modifiers] can generally be same as for the Get(int) method. If you can't edit the Questions class or for some reason don't want to, you can overload the method by writing an extension:

    public static class Extensions
    {
        public static Questions Get(this Questions qs, Question q)
        {
            return qs.Get((int)q);
        }
    }
    

提交回复
热议问题