Return Enum From C# Property getters

前端 未结 7 890
谎友^
谎友^ 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 14:49

    I think you may have misunderstood the reasons for using enumerations. An enumeration is a special type of variable that can only take a specific set of values. For example. a boolean is a very special case of enumeration. A boolean variable is one that can only take 2 values - Either "true" or "false".

    You have defined an enumeration:

    public enum LowerCase
        {
            a,
            b,
        }

    This means that a variable of type LowerCase will only be able to be set to either a, or b. This isn't really much use.

    Your other enumeration is more sensible:

    public enum Case
        {
            Upper,
            Lower,
        }
    A variable of type Case can be set to either "Upper" or "Lower". Now when you define a variable of an enumeration type, you specify the type you want it to be. You have done this:
    private Enum case;
    but what think you mean is:
    private Case case;
    This defines a private variable of type Case called case. The enum keyword is only used when you are defining a new enumeration, not when you are defining a variable of an enumeration type.

    Similarly, where you have defined your property, you should use the enum type that you want to return:

    public Case ChooseCase
    {...
    And where you are trying to return a enumeration value, you should be doing this:
    return Case.Lower

提交回复
热议问题