zoukankan      html  css  js  c++  java
  • C# const常量详细介绍

      这里介绍C# const常量,使用Visual C#在 Main()里面使用IntelliSence插入Constant的相关field的时候,发现ReadonlyInt和 InstantReadonlyInt需要指定Constant的实例对象。

      C#语言有很多值得学习的地方,这里我们主要介绍C# const常量,包括介绍readonly和const所修饰的变量等方面。

       

      一般情况下,如果你需要声明的常量是普遍公认的并作为单个使用,例如圆周率,黄金分割比例等。你可以考虑使用C# const常量,如:public const double PI = 3.1415926;。如果你需要声明常量,不过这个常量会随着实际的运行情况而决定,那么,readonly常量将会是一个不错的选择,例如上面第一个例子的订单号Order.ID。

       

      另外,如果要表示对象内部的默认值的话,而这类值通常是常量性质的,那么也可以考虑const。更多时候我们对源代码进行重构时(使用Replace Magic Number with Symbolic Constant),要去除魔数(Magic Number)的影响都会借助于const的这种特性。

       

      对于readonly和const所修饰的变量究竟是属于类级别的还是实例对象级别的问题,我们先看看如下代码:

       

      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. }  

      使用Visual C#在 Main()里面使用IntelliSence插入Constant的相关field的时候,发现ReadonlyInt和 InstantReadonlyInt需要指定Constant的实例对象;而ConstInt和StaticReadonlyInt却要指定 Constant class(参见上面代码)。可见,用const或者static readonly修饰的常量是属于类级别的;而readonly修饰的,无论是直接通过赋值来初始化或者在实例构造函数里初始化,都属于实例对象级别。

       

      一般情况下,如果你需要表达一组相关的编译时确定常量,你可以考虑使用枚举类型(enum),而不是把多个C# const常量直接嵌入到class中作为field,不过这两种方式没有绝对的孰优孰劣之分。

       

      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. }  

  • 相关阅读:
    request.json 打印中文乱码解决
    看懂项目代码需要掌握的技能 (java语言)
    jmeter响应断言通过,结果树中却显示红色
    nginx的upstream后端名称居然变成了请求的host了?
    基于QRcode创建和识别二维码的研究
    thinkphp访问mysql中文字段问题
    apache https访问配置
    如何获得bibitem格式的参考文献
    CSharp: Image Matting
    word2vec回顾
  • 原文地址:https://www.cnblogs.com/zcy_soft/p/1773630.html
Copyright © 2011-2022 走看看