The const modifier is a (class) static constant, whose value is a dynamic constant that is determined during compilation by the ReadOnly adornment.
A, the difference between Const and readonly in C #
There are two ways to define constants in C #, a static constant, defined with the "const" keyword (that is, const = static const), a const-defined value is determined during compilation, and its value can be specified only at declaration time through a constant expression. The other is called Dynamic constants, which are defined by the "readonly" keyword. The two differences are as follows:
1. Const can only modify primitive type, enum type, or string type, that is, restrict const type to be of value type range, and its value cannot be set by new, ReadOnly no Limit;
2. Const can be used to modify class field or a local variable (local variable), while ReadOnly is only used to modify class field;
3. Const constants belong to the class level, not the instance object level, and the ReadOnly constant can be either a class level or an instance object level;
4. Const constants are more efficient and do not occupy memory space. Const constants are compiled by the compiler, where the const variable is referenced in code in place of the actual value corresponding to the const variable. The ReadOnly constant requires the system to allocate space for the constants it defines.
B, C # Enumeration and the application of the constant difference, the original address: http://developer.51cto.com/art/200908/144474.htm
What are the differences between C # enumerations and constant applications?
When we need to define it, the enumeration is given precedence.
In C #, the real power of enumerations is that they are instantiated in the background as a struct that derives from the base class System.Enum. This means that you can invoke methods on them and perform useful tasks. Note that because of the way the. NET framework is executed, there is no performance penalty for the syntax of enumerating enumerations as structs. In fact, once the code is compiled, the enumeration becomes the base type, similar to int and float.
But in practical applications, you might find that we often define enumeration types in English, because development tools are developed in English, and Americans use them to understand the meaning of enumerated types directly. In fact, we have one more step in the development, we need to translate the enumeration type. No way, who let the programming language is written in English, if it is written in Chinese, then we do not have to translate, with the enumeration became very convenient. To give a simple example, timeofday.morning see morning, Americans know is the morning, but for Chinese users, there may be many people can not understand, which requires us to translate, explain, to the above the Gettimeofday () method, is actually doing translation work. Therefore, when using enumerations, it is not very convenient to feel, sometimes we are more willing to create constants, and then in the class, declare a collection to accommodate the constant and its meaning.
C # enumerations and constants are defined using constants: Although this method is feasible, it is not guaranteed that the passed-in Parameters day is actually qualified.
- Using System;
- Using System.Collections.Generic;
- C # Enumerations and constants application differences
- Public class TimesOfDay
- {
- Public const int morning = 0;
- Public const int afternoon = 1;
- Public const int Evening = 2;
- Public static Dictionary﹤int, string﹥list;
- ﹤summary﹥
- Get the day of the week
- ﹤/summary﹥
- ﹤param name= "Day" ﹥﹤/param﹥
- ﹤returns﹥﹤/returns﹥
- Public static string gettimenameofday (int time)
- {
- if (list = = Null | | list. count﹤= 0)
- {
- List = new Dictionary﹤int, String﹥ ();
- List. Add (morning, "Morning");
- List. Add (afternoon, "PM");
- List. ADD (Evening, "Night");
- }
- return List[time];
- }
- }
Hopefully we can find a better way to convert the enumeration to the set we want. Search for half a day finally found some clues. A description of an enumeration type is obtained by reflection.
C # Enumerations and constants apply a description to the definition of the enumeration that applies the difference
- Using System;
- Using System.ComponentModel;
- C # Enumerations and constants application differences
- Public enum TimeOfDay
- {
- [Description ("Morning")]
- Moning,
- [Description ("Afternoon")]
- Afternoon,
- [Description ("Night")]
- Evening,
- };
C # Enumerations and constants apply the difference between the obtained value and the stated key-value pair
- ﹤summary﹥
- Reads from the enumeration type and its attributes and returns a key-value pair
- ﹤/summary﹥
- ﹤param name= "Enumtype" ﹥
- Type, the format of the parameter is typeof (enumeration type that needs to be read)
- ﹤/param﹥
- ﹤returns﹥ Key value pair ﹤/returns﹥
- Public Static NameValueCollection
- Getnvcfromenumvalue (Type enumtype)
- {
- NameValueCollection NVC = new NameValueCollection ();
- Type typedescription = typeof (DescriptionAttribute);
- System.reflection.fieldinfo[]
- Fields = Enumtype.getfields ();
- String strText = string. Empty;
- String strvalue = string. Empty;
- foreach (FieldInfo field in fields )
- {
- if (field. Fieldtype.isenum)
- {
- strvalue = ((int) Enumtype.invokemember (
- Field. Name, Bindingflags.getfield, null,
- NULL, NULL )). ToString ();
- object[] arr = field. GetCustomAttributes (
- Typedescription, true);
- if (arr. LENGTH﹥0)
- {
- DescriptionAttribute AA =
- (DescriptionAttribute) arr[0];
- StrText = AA. Description;
- }
- Else
- {
- StrText = field. Name;
- }
- Nvc. ADD (StrText, strvalue);
- }
- }//c# enumeration and constants apply differences
- return NVC;
- }
Of course, the enumeration definition can also be Chinese, very simple to solve the above problem, but our code does not look like a unified language.
- Chineseenum
- Public enum TimeOfDay
- {
- Morning
- Afternoon
- At night
- }
C, C # gets the key name, value, and description of the enumeration, iterating through the enumeration
The operation of the C # Enum enumeration. Key names, values and descriptions, and traversal enumerations
<summary>
Promotion
</summary>
public enum Cxsd
{
[Description ("recommended")]
TJ = 2,
[Description ("pinned")]
ZD = 4,
[Description ("Hot sale")]
RM = 8
}
Get enumeration values
Array Rolearry = Enum.getvalues (typeof (Cxsd));
Get enumeration Name
String[]rolearry = Enum.getnames (typeof (Cxsd));
Get enumeration Description
Descriptions (cxsd.rm);//Call
public static string description (enum en)
{
&NBSP ; Type type = en. GetType ();
memberinfo[] Meminfo = type. GetMember (en. ToString ());
if (meminfo! = null && meminfo.length > 0)
&NBSP ; {
object[] attrs = meminfo[0]. GetCustomAttributes (typeof (System.ComponentModel.DescriptionAttribute), false);
if (attrs! = null && attrs. Length > 0)
return ((DescriptionAttribute) attr S[0]). Description;
}
return en. ToString ();
}
Traversal enumeration
Type type = typeof (Cxsd); foreach (FieldInfo x in type. GetFields (BindingFlags.Public | bindingflags.static) { cxsd item = (cxsd) x.getvalue (null); }
Extend a common method with the above method. To get the descriptive information for the specified value
Calling methods
List<int> vlist=new list<int> ();
Vlist.add (4);
Vlist.add (8);
Descriptions<cxsd> (Vlist);
<summary>
Get description Information
</summary>
<param name= "Envalue" > Collection of enumeration values </param>
<returns> enumeration values corresponding to the description collection </returns>
public static list<string> descriptions<t> (list<int> envalue)
{
Type type = typeof (T);
list<string> deslist = new list<string> ();
foreach (FieldInfo x in type. GetFields (BindingFlags.Public | bindingflags.static))
{
T item = (t) x.getvalue (NULL);
if (Envalue. Find (int e) = {return E = = Convert.ToInt32 (item);}) > 0)
{
Deslist. ADD (description<t> (item));
}
}
return deslist;
}
public static string description<t> (T en)
{
Type type = en. GetType ();
memberinfo[] Meminfo = type. GetMember (en. ToString ());
if (meminfo! = null && meminfo.length > 0)
{
object[] Attrs = meminfo[0]. GetCustomAttributes (typeof (System.ComponentModel.DescriptionAttribute), false);
if (attrs! = null && attrs. Length > 0)
Return ((DescriptionAttribute) attrs[0]). Description;
}
Return en. ToString ();
}
C # enumerations (enum), constants (const), and ReadOnly