다음을 통해 공유


C#: Implement enum multi-functional programming method

Before writing programs, we often define the use of some constants, and then focus on a Class above.

class Window
{
 int Close = 0;
 int Open = 1;
}

But in fact the effectiveness and memory in the use of a little fat (if the use of a large number of words), then the evolution of the program, found that such a demand for more, because the same type we can directly control, and then define a Like, and later evolved Enum such a type of existence. For example, you may need a similar property option called "level" and can only be assigned a type of "A", "B", "C", "F", and any other value in this type Is illegal.

Enum is the first use of Java a type, then C # also began to follow up.

Enum is known as the "list of types", for many beginners, they know that the list of types, will also use, but do not necessarily know where the list of types used, what easy to use. So in this article, in addition to you to introduce Enum how to use, but also tell you it's easy to use.

Enum can be used to define constants.

Enum's announcement

In .Net Enum use is very easy, very intuitive (in fact, with you in the use of class directly defined like):

enum windowStatus : int
{
 Close,
 Open
};

In fact, Enum is a very special type, when you declare an Enum type, it is actually and set up a group of enumerated digital constants no different. We can also directly to specify the Enum series of individual values:

enum windowStatus : int
{
 Close=0,
 Open=1
};

Above is a basic use, but to. Net current evolution, or with Java a little different, for example:

public enum Window {
 
 close(0, "Close"), open (1, "Open") ;
 
 int value = 0;
 String text = "";
 
 private Window(int value, String text) {
 this.value = value;
 this.text = text;
 }
 
 public int getValue() {
 return this.value;
 }
 
 public String getText() {
 return this.text;
 }
 
 public static Window ofValue(int value) {
 for (LawMasterRole r : values()) {
 if (r.getValue() == value) {
 return r;
 }
 }
 return undefined;
 }
 
}

The above example we can see that Java can be added in the enum method. And Java Enum under the values () method, this method is to take Enum all the values, it is wonderful. I went to check the JAVA DOC java.lang.Enum <ElementType> do not have this method, the method in the java.lang.annotation.ElementType category (public enum ElementType extends Enum <ElementType>). Enum's declaration is: Enum <? Extends Enum <E >>, that is, after the generic must be Enum sub-category. So reasonable reasoning is this: in the interpretation of it will see a Test, Test is Enum sub-category, ElementType is Enum sub-category. Why ElementType has Values () and valueOf () methods, because ElementType itself is also a subclass of Enum, and these methods are automatically added at compile time. Any category is not like the subclass of ElementType, because any enum compiler is final modified, unless it's a enum class body, and ElementType no, final modified category can not inherit, it is the compiler automatically generated of.

These are the current C # Enum not, a hypothesis is that we designed an Enum.

enum windowStatus : int
{
 Close = 0,
 Open
};

At this time we are in use, in general, there is no big problem, that is, straight out the Enum parameters inside, and it is int wrong. But the human needs are endless, this time we would like to say that it can also be achieved side of the corresponding int, you can take out its name?

Of course, in fact, we can do an Enum to achieve some of their own methods, for example, self-defined Enum entries in the string name, and remove its value:

public sealed class Window
{
 
 private string name;
 private int value;
 public static readonly Window Close = new Window("Close",0);
 public static readonly Window Open = new Window("Open", 1);
 
 private Window(string name, int value)
 {
 this.name = name;
 this.value = value;
 }
 
 public override string ToString()
 {
 return this.name;
 }
}

The above is also able to render Enum into String, but if you have actually run like this, you will find a problem.

That is the result will always be presented only String, and will not show the value of class itself. 

Console.WriteLine("Window Enum:" + Window.Close);
Console.WriteLine("Window Enum ToString:" + Window.Close.ToString());
Console.WriteLine("Window Enum:" + Window.Open);
Console.WriteLine("Window Enum ToString:" + Window.Open.ToString());

https://4.bp.blogspot.com/-RPy-gkRkLlA/WaWSAFyIIdI/AAAAAAAAAz8/1M2XtqnB2AYdk_B9zacnrJQMrw6vL4ccgCLcBGAs/s1600/enum1.jpg

The other key place is override string ToString (). This place will be an object after the definition of this type of type. So I leave a few parameters, the result is only to show a type, so do not use ToString no effect, but the mountain does not turn into, we can write like this:

public sealed class Window
{
 
 private string name;
 private int value;
 public static readonly Window Close = new Window("Close",0);
 public static readonly Window Open = new Window("Open", 1);
 
 private Window(string name, int value)
 {  
 this.name = name;
 this.value = value;
 }
 
 public override string ToString()
 {
 return this.value.ToString();
 }
 
 public string GetEnumName()
 {
 return this.name;
 }
}

See! I just want to do a little hands and feet, is to place ToString value of the use of value, and add a Funtion practice just fine, the saying goes, everything is not too persistent, we do not have to change that no way Moving ToString, as long as the addition of a Function, you can also achieve Enum both conditions, what kind of conditions.

Console.WriteLine("Window Enum:" + Window.Close);
Console.WriteLine("Window Enum ToString:" + Window.Close.ToString());
Console.WriteLine("Window Enum GetEnumName:" + Window.Close.GetEnumName());
Console.WriteLine("Window Enum:" + Window.Open);
Console.WriteLine("Window Enum ToString:" + Window.Open.ToString());
Console.WriteLine("Window Enum GetEnumName:" + Window.Open.GetEnumName());

https://3.bp.blogspot.com/-HnRXPmlve7o/WaWUtQCj-nI/AAAAAAAAA0Q/YeXhX7tavek3nkUcxJ1ZVb33GtSUt57IQCLcBGAs/s1600/enum2.jpg

And then we as long as the value of the variables into the move, you can let Window with Enum, set its type.

public sealed class Window
{
 private string name;
 private object value;
 public static readonly Window Close = new Window("Close",0);
 public static readonly Window Open = new Window("Open", 1);
 
 private Window(string name, object value)
 {  
 this.name = name;
 this.value = value;
 }
 
 public override string ToString()
 {
 return this.value.ToString();
 }
 
 public string GetEnumName()
 {
 return this.name;
 }
}

As long as it is set to object, you can let it bring any objects come in, in this example, you can achieve the same function as Java. This example can actually have a lot of changes, of course, every kind of writing, have its advantages and disadvantages, as well as the effectiveness of the problem, to see how they have to implement it. 

Here's my example download link:

https://code.msdn.microsoft.com/Implement-enum-multi-b371f5cc

Back to Top