• Const 和 readonly


    const 关键字用于修改字段或局部变量的声明。 它指定字段或局部变量的值是常数,不能被修改。

    例如:

    public const double gravitationalConstant = 6.673e-11;

    只有 C# 内置类型(System.Object 除外)可以声明为 const 下表列出了C# 内置类型:

    bool

    System.Boolean

    byte

    System.Byte

    sbyte

    System.SByte

    char

    System.Char

    decimal

    System.Decimal

    double

    System.Double

    float

    System.Single

    int

    System.Int32

    uint

    System.UInt32

    long

    System.Int64

    ulong

    System.UInt64

    object

    System.Object

    short

    System.Int16

    ushort

    System.UInt16

    string

    System.String

    用户定义的类型(包括类、结构和数组)不能为 const 请使用 readonly 修饰符创建在运行时初始化一次即不可再更改的类、结构或数组。

    对于引用类型的常量,除string外,其他的值只能是null

    如下:

    class Test

    {

    // below wrong.

    public const Test2 T2 = new Test2();

    // below right, can only be initialized with null for reference type except string.

    这种写法虽然编译通过,但其实没有任何意义了,所以说对类不声明为const.

    public const Test2 T3 = null;

    }

    class Test2

    {

    }

    将产生编译错误:

    A const field of a reference type other than string can only be initialized

    with null.

    虽然 const 字段是编译时常量,但 readonly 字段可用于运行时常量,如此行所示:public static readonly uint l1 = (uint)DateTime.Now.Ticks;

    const常量通过类名访问,不允许在常数声明中使用 static 修饰符。

    Difference between static readonly and const:

    The difference is that the value of a static readonly field is set at run time, and can thus be modified by the containing class, whereas the value of a const field is set to a compile time constant.

    In the static readonly case, the containing class is allowed to modify it only

    • in the variable declaration (through a variable initializer)
    • in the static constructor (instance constructors, if it's not static)

    static readonly is typically used if the type of the field is not allowed in a const declaration, or when the value is not known at compile time.

    Instance readonly fields are also allowed. 

    Remember that for reference types, in both cases (static and instance) the readonly modifier only prevents you from assigning a new reference to the field.  It specifically does not make immutable the object pointed to by the reference.

    这里说的是对于引用类型,readonly只是保证你不能给它赋新的引用,但是它所指向的值是可以改变的。

    class Program
    {
    static void Main(string[] args)
    {
    var t
    = new MyClass();
    t.Add();
    }
    }

    public class MyClass
    {
    private readonly Collection<string> _str = new Collection<string>();
    public MyClass()
    {
    _str.Add(
    "a");
    _str.Add(
    "b");
    _str.Add(
    "c");
    }

    public void Add()
    {
    _str.Add(
    "d");
    foreach (var s in _str)
    Debug.WriteLine(s);
    }
    }

     这样添加是成功的,最后输出:

    a

    b

    c

    d

  • 相关阅读:
    C# 实现向指定邮箱发送信息功能
    asp.net webapi 解决跨域问题
    电脑通电自动开机设置
    C# 多个控件绑定一个事件
    C# DataGridView 标题栏背景颜色改变
    C# 输出csv文件后缀乱码
    C# textbox设定为只读后如何改变文本字体颜色
    C# 命名规则
    C# 傅里叶变换 逆变换 调用MathNet包
    使用SharpDevelop配合MonoGame进行游戏开发
  • 原文地址:https://www.cnblogs.com/bear831204/p/2152291.html
Copyright © 2020-2023  润新知