Return Enum From C# Property getters

前端 未结 7 889
谎友^
谎友^ 2021-01-21 14:35

How do you return an Enum from a setter/getter function?

Currently I have this:

    public enum LowerCase
    {
        a,
        b,
    }
    public en         


        
相关标签:
7条回答
  • 2021-01-21 15:08

    Probably you should use a base class Case and two derived classes, so that your ChooseCase property returns an object of the corresponding class:

    abstract class Case
    {
        abstract char GetItem(int index);
    }
    
    class LowerCase : Case
    {
        override char GetItem(int index)
        {
            // Not sure for the following
            Encoding ascii = Encoding.ASCII;
            return ascii.GetString(ascii.GetBytes("a")[0] + index)[0];
        }
    }
    
    class UpperCase : Case
    {
        // The same for the upper case
    }
    
    public Case ChooseCase
    {
        get
        {
            if (condition)
            {
                return new LowerCase();
            }
            else
            {
                return new UpperCase();
            }
        }
    }
    

    This is a thing you cannot do with enums, because they all derive exactly from Enum.

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