Methods inside enum in C#

后端 未结 6 1332
离开以前
离开以前 2020-12-04 10:21

In Java, it\'s possible to have methods inside an enum.

Is there such possibility in C# or is it just a string collection and that\'s it?

I tried to override

6条回答
  •  不思量自难忘°
    2020-12-04 11:08

    Nope. You can create a class, then add a bunch of properties to the class to somewhat emulate an enum, but thats not really the same thing.

    class MyClass
    {
        public string MyString1 { get{ return "one";} }
        public string MyString2 { get{ return "two";} }
        public string MyString3 { get{ return "three";} }
    
        public void MyMethod()
        {
            // do something.
        }
    }
    

    A better pattern would be to put your methods in a class separate from your emum.

提交回复
热议问题