Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # const constant

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use C # const constant". In daily operation, I believe many people have doubts about how to use C # const constant. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use C # const constant". Next, please follow the editor to study!

In general, if you need to declare a constant that is generally accepted and used as a single, such as pi, golden section ratio, etc. 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 depend on the actual operation, then the readonly constant will be a good choice, such as the order number Order.ID in the above example.

In addition, if you want to represent default values within an object, and such values are usually constant, you can also consider const. More often than not, when we ReFactor the source code (using Replace Magic Number with Symbolic Constant), we rely on this feature of const to remove the influence of Magic Number.

For the question of whether 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 you use Visual C # to insert the relevant field of Constant using IntelliSence in Main (), you find that ReadonlyInt and InstantReadonlyInt need to specify the instance object of Constant, while ConstInt and StaticReadonlyInt need to specify Constant class (see the code above). It can be seen that constants modified with const or static readonly belong to the class level, while readonly decorations, whether initialized directly through assignment or in the instance constructor, belong to the instance object level.

In general, if you need to express a set of related compile-time deterministic constants, you can consider using enumerated types (enum) instead of embedding multiple C # const constants directly into class as field, but there is no absolute difference between the two ways.

Using System; enum CustomerKind {SuperVip, Vip, Normal} class Customer {public Customer (string name, CustomerKind kind) {m_Name = name; m_Kind = kind;} private string name; public string Name {get {return name;} private CustomerKind masked Kindle; public CustomerKind Kind {get {return m_Kind }} public override string ToString () {return "Name:" + m_Name + "[" + m_Kind.ToString () + "]";}} at this point, the study on "how to use the C# const constant" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report