欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

C# : Enum and overriding ToString on it,

来源: javaer 分享于  点击 31130 次 点评:158

C# : Enum and overriding ToString on it,


  

I saw two posts on Enums today on Eric Lipperts and Chris Rathjen's blog. Enums are significantly different from the other types and people run into unusal problems while working with them.

C++

The CLI enum is considerably different from the native C++ enum and therefore you need to be careful how you define and use them in Managed C++. In new syntax (C++/CLI whidbey) the following code compiles even though both the enums define the same name Cool.

In old syntax (pre whidbey) you need to define the enum as __value enum Coolness. In case you drop the class then you are using the native C++ enum and according to C++ specification the enumerator declarations are promoted to the same scope as the enumerated type declaration and the code will fail to compile pointing out a redefinition of Cool.

C#

In C# we do need not care about these as there is only one definition of enum which is that of the CLI enum.

Common things like the first enum member get the value 0, the value of a subsequent member is calculated by adding 1 to the member textually preceeding it, you can assign values explicitely to a enum member are well-known. However there are some not so well known usages as well.


By default the compiler uses Int32 to store the enum members. Here we are asking it to use byteVeryCool uses a reference to the NotSoCool and will get the value 12. So the out put of the code above will be

Underlying type: System.Byte
Type Code      : Byte
Value          : 12

Since all enums have System.Enum as the abstract base type, a lot of funcionality becomes available to get details about the enum type.

If you want to print the value of the enum then it can be done in the following ways

Output:
VeryCool VeryCool 0C 12

F and G gives the same value in this case. They differ based on whether FlagsAttribute is applied to it.

You can also get a array filled with either the value (GetValues) or names (GetNames) of all the constants in the enum.


This prints

NotSoCool  => 5
Cool       => 6
VeryCool   => 12
SuperCool  => 13

You can also query the name of the constant in the enum that has the specified value or whether a value is defined in the enum or not. The following will print cool and 5 is not defined.


Overriding ToString()

You cannot override the ToString of the enum type. So in case you wanted to display "Not so cool" instead of NotSoCool when someone called ToString on your enum type then you cannot do that simply by overriding the ToString.

This is a common issue that comes up frequently when you want to show values in reports, web pages, XML where you want to put in human readable text for enum values. Commonly people use non-generic solution of maintaining arrays of these descriptions and get text out of them by indexing using the enum value or some other things like storing it in a hashtable and using the ToString value as the key to get the desciption out.

A generic solution would be to apply custom attributes on the enum constants and write static methods to get the desciption. See the modified code below


Using this approach is pretty generic because for all enum constants that have this attribute applied to it, the desciption will be picked up from it and for those that do not have the attribute the common ToString() method will be called to get the description. However, the GetDescription uses reflection and can be slow.

 

 

From:http://www.cnblogs.com/jxnclyk/archive/2010/04/02/1702902.html

 

相关文章

    暂无相关文章

用户点评