C # const Constants

Source: Internet
Author: User
    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:

     

     

 
 
  1. using System;  
  2.  
  3. namespace ConstantLab  
  4. {  
  5. class Program  
  6. {  
  7. static void Main(string[] args)  
  8. {  
  9. Constant c = new Constant(3);   
  10. Console.WriteLine("ConstInt = " + Constant.ConstInt.ToString());   
  11. Console.WriteLine("ReadonlyInt = " + c.ReadonlyInt.ToString());   
  12. Console.WriteLine("InstantReadonlyInt = " + c.InstantReadonlyInt.ToString());   
  13. Console.WriteLine("StaticReadonlyInt = " + Constant.StaticReadonlyInt.ToString());   
  14.  
  15. Console.WriteLine("Press any key to continue");  
  16. Console.ReadLine();  
  17. }  
  18. }  
  19.  
  20. class Constant  
  21. {  
  22. public Constant(int instantReadonlyInt)  
  23. {  
  24.  InstantReadonlyInt = instantReadonlyInt;   
  25. }  
  26.  
  27. public const int ConstInt = 0;  
  28.  
  29. public readonly int ReadonlyInt = 1;  
  30.  
  31. public readonly int InstantReadonlyInt;  
  32.  
  33. public static readonly int StaticReadonlyInt = 4;  
  34. }  

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.

 

 

 
 
  1. using System;  
  2.  
  3. enum CustomerKind  
  4. {  
  5. SuperVip,  
  6. Vip,  
  7. Normal  
  8. }  
  9.  
  10. class Customer  
  11. {  
  12. public Customer(string name, CustomerKind kind)  
  13. {  
  14.  m_Name = name;  
  15.  m_Kind = kind;  
  16. }  
  17.  
  18. private string m_Name;  
  19. public string Name  
  20. {  
  21. get { return m_Name; }  
  22. }  
  23.  
  24. private CustomerKind m_Kind;  
  25. public CustomerKind Kind  
  26. {  
  27. get { return m_Kind; }  
  28. }  
  29.  
  30. public override string ToString()  
  31. {  
  32. return "Name: " + m_Name + "[" + m_Kind.ToString() + "]";  
  33. }  

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.