-2
public enum Mode
{
    Cloud = 1,
    Local = 2
}

    private static Mode _applicationMode;

    public static Mode ApplicationMode(string mode)
    {
        get
        {
            {
                _applicationMode = Mode.Cloud;
                Mode appMode;
                if (Enum.IsDefined(typeof(Mode), mode))
                {
                    _applicationMode = (Mode)Enum.Parse(typeof(Mode), mode);
                }
                else if (Enum.TryParse(mode, true, out appMode))
                {
                    _applicationMode = appMode;
                }
            }

            return _applicationMode;
        }
    }

Here I am pass "ABC" as a parameter for ApplicationMode function then it returns _applicationMode as 0(default value of AppMode enum)

If I pass "123" as a parameter for ApplicationMode function. But it returns _applicationMode as a "123".

Why Enum is accepting the "123" even though AppMode Enum has only two values i.e., Local and Cloud?

TryParse should give false for "123" also but it gives true for "123".

Yogesh Patel
  • 818
  • 2
  • 12
  • 26
  • 2
    The code you've posted doesn't compile (you have a `get` property inside a static method?!). Please can you post either your actual code or, preferably, an [MVCE](http://stackoverflow.com/help/mcve) illustrating this issue. – RB. Apr 01 '16 at 10:08
  • I agree with @RB, also share some code that calls your `ApplicationMode` method, for instance in `Console.WriteLine` calls, and share the actual output of those calls. – Sam Bauwens Apr 01 '16 at 10:10
  • Both Properties are in static class. I am using this as below if (ApplicationMode == Mode.Local) { } – Yogesh Patel Apr 01 '16 at 10:11
  • @yogeshpatel Still not gonna let the code you posted compile! If you want help you will need to follow my advice and post a proper MVCE. – RB. Apr 01 '16 at 10:13
  • Let me clarify that - you have never read the documentation and now have expectations to TryParse that DIRECTLY contraditct the documentation, and you are not willing to spend enough time to make even a case here that compiles? And you ask for free help? – TomTom Apr 01 '16 at 10:18
  • @yogeshpatel Enums are aliases over their underlying types. You *can* use an arbitrary integer instead of a defined enum value. `(Mode )4567` is valid. If TryParse behaved differently it would actually break the expected behaviour of Enums. The actual problem in your case though is that a numeric *string* was passed where a name was expected. – Panagiotis Kanavos Apr 01 '16 at 11:21

1 Answers1

2

Enum.TryParse will accept any integer whether a member of the enum or not. From the documentation:

If value is the string representation of an integer that does not represent an underlying value of the TEnum enumeration, the method returns an enumeration member whose underlying value is value converted to an integral type. If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of TEnum.

Stuart Whitehouse
  • 1,421
  • 18
  • 30