Here we will introduce the C # const constant. When using Visual C # To use intelliisence in main () to insert fields related to constant, we will find that readonlyint and instantreadonlyint need to specify the instance object of constant.
C # language is worth learning a lot. Here we mainly introduce C # const constants, including the variables modified by readonly and Const.
In general, if you need to declare constants that are universally recognized and used as a single, such as the circumference rate, golden split ratio, and so on. You can consider using C # const constants, such as public const double Pi = 3.1415926 ;. If you need to declare a constant, but this constant will be determined according to the actual running situation, the readonly constant will be a good choice, such as the order number order in the first example above. id.
In addition, if you want to represent the default value inside the object, and such values are usually constant properties, you can also consider Const. More often, when we refactor the source code (using replace magic number with symbolic constant), the impact of the magic number will be attributed to this feature of Const.
If the variables modified by readonly and const belong to the class level or the Instance Object level, let's take a look at the following code:
- using System;
-
- namespace ConstantLab
- {
- class Program
- {
- static void Main(string[] args)
- {
- Constant c = new Constant(3);
- Console.WriteLine("ConstInt = " + Constant.ConstInt.ToString());
- Console.WriteLine("ReadonlyInt = " + c.ReadonlyInt.ToString());
- Console.WriteLine("InstantReadonlyInt = " + c.InstantReadonlyInt.ToString());
- Console.WriteLine("StaticReadonlyInt = " + Constant.StaticReadonlyInt.ToString());
-
- Console.WriteLine("Press any key to continue");
- Console.ReadLine();
- }
- }
-
- class Constant
- {
- public Constant(int instantReadonlyInt)
- {
- InstantReadonlyInt = instantReadonlyInt;
- }
-
- public const int ConstInt = 0;
-
- public readonly int ReadonlyInt = 1;
-
- public readonly int InstantReadonlyInt;
-
- public static readonly int StaticReadonlyInt = 4;
- }
- }
When using Visual C # To insert fields related to Constant Using intelliisence in main (), we find that the instance objects of readonlyint and instantreadonlyint must be specified; constint and staticreadonlyint must specify the constant class (see the code above ). It can be seen that the constant modified with const or static readonly belongs to the class level, while the readonly variable can be initialized directly by assigning values or initialized in the instance constructor, all belong to the Instance Object level.
In general, if you need to express a set of related constants at compilation, you can consider using the enumeration type (Enum ), instead of embedding multiple C # const constants directly into the class as the field, there is no absolute superiority or inferiority between the two methods.
- using System;
-
- enum CustomerKind
- {
- SuperVip,
- Vip,
- Normal
- }
-
- class Customer
- {
- public Customer(string name, CustomerKind kind)
- {
- m_Name = name;
- m_Kind = kind;
- }
-
- private string m_Name;
- public string Name
- {
- get { return m_Name; }
- }
-
- private CustomerKind m_Kind;
- public CustomerKind Kind
- {
- get { return m_Kind; }
- }
-
- public override string ToString()
- {
- return "Name: " + m_Name + "[" + m_Kind.ToString() + "]";
- }
- }