const 与 static readonly的区别

google到一篇讲const 与 static readonly的文章 还不错~草译一下

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.
被 static readonly修饰的字段 是在运行时中被设置值,可以修改. const 修饰的字段是在编译时被设置值。
(const 修饰的字段的值直接被写进IL中间码里面)

In the static readonly case, the containing class is allowed to modify it only
只有 static readonly 字段的包含类才可以对其进修赋值修改

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.

    class Program
    {
        public static readonly Test test = new Test();
        static void Main(string[] args)
        {
            test.Name = "Program";
            test = new Test();  // Error:      A static readonly field cannot be assigned to (except in a static constructor or a variable initializer) 
        }
    }
    class Test
    {
        public string Name;
    }

On the other hand, if Test were a value type, then assignment to test.Name would be an error. 

时间: 2024-08-22 15:26:42

const 与 static readonly的区别的相关文章

C# FAQ: const和static readonly有什么区别?

ado|区别 我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值.而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值.明白了这个本质区别,我们就不难看出下面的语句中static readonly和const能否互换了:1. static readonly MyClass myins =

C#中 const 和 readonly 的区别

ado|区别 const 和 readonly 的区别,总是不太清楚,于是查了查资料.   const 的概念就是一个包含不能修改的值的变量.常数表达式是在编译时可被完全计算的表达式.因此不能从一个变量中提取的值来初始化常量.如果 const int a = b+1;b是一个变量,显然不能再编译时就计算出结果,所以常量是不可以用变量来初始化的.   readonly 允许把一个字段设置成常量,但可以执行一些运算,可以确定它的初始值.因为 readonly 是在计算时执行的,当然它可以用某些变量初

解析php中static,const与define的使用区别_php技巧

define部分:宏不仅可以用来代替常数值,还可以用来代替表达式,甚至是代码段.(宏的功能很强大,但也容易出错,所以其利弊大小颇有争议.)宏的语法为:#define 宏名称 宏值作为一种建议和一种广大程序员共同的习惯,宏名称经常使用全部大写的字母.利用宏的优点:1)让代码更简洁明了当然,这有赖于你为宏取一个适当的名字.一般来说,宏的名字更要注重有明确直观的意义,有时宁可让它长点.2)方便代码维护对宏的处理,在编译过程中称为"预处理".也就是说在正式编译前,编译器必须先将代码出现的宏,用

java-基础-Interface、abstract类、Static class 、non static class的区别

Interface与abstract类的区别. 抽象类和接口都不能够实例化,但可以定义抽象类和接口类型的引用.一个类如果继承了某个抽象类或者实现了某个接口都需要对其中的抽象方法全部进行实现,否则该类仍然需要被声明为抽象类.接口比抽象类更加抽象,因为抽象类中可以定义构造器,可以有抽象方法和具体方法,而接口中不能定义构造器而且其中的方法全部都是抽象方法.抽象类中的成员可以是private.默认.protected.public的,而接口中的成员全都是public的.抽象类中可以定义成员变量,而接口中

visual studio-using和static有什么区别,using static必须是static class么?

问题描述 using和static有什么区别,using static必须是static class么? using和static有什么区别,using static必须是static class么?vs2015 解决方案 using static 一个静态类,可以不通过类名调用: using static System.Console; using static System.Math; Console.WriteLine(Math.Sin(1.23)); 可以写 WriteLine(Sin(

IOS 关键字const 、static、extern详解_IOS

IOS 关键字const .static.extern详细介绍: 一.前言 阅读别人的代码(一些优秀的源码)总能发现一些常见的关键字,随着编程经验的积累大部分还是知道是什么意思的,但在概念上和具体的用法上却依然有些模糊,故特意整理汇总了下三个比较常见的关键字--const/static/extern. 二.关键字const/static/extern的释义和用法 1.const 这个单词翻译成中文是"常量"的意思.在程序中我们知道"常量"的值是不能变的,固定的.所以

区分const,static,readonly,volatile四个关键字

const:表示常量,变量的值是绝不会被改变的,常量的值是在编译时就已经确定了.编译器会把常量的值保存在程序集的元素据里面,在C#里面,下面列举的简单类型才能被定义为 常量:Boolean,  Char,  Byte,  SByte,  Int16,  UInt16 ,  Int32,  UInt32 ,  Int64, UInt64 ,  Single ,  Double ,  Decimal, String.如果定一个引用类型为常量,则必须把该变量的值设为null.因为常量的值是不会改变的,

const extern static

const const最好理解,修饰的东西不能被修改 指针类型根据位置的不同可以理解成3种情况: I 常量指针 // 初始化之后不能赋值,指向的对象可以是任意对象,对象可变. NSString * const pt1; II 指向常量的指针 // 初始化之后可以赋值,即指向别的常量,指针本身的值可以修改,指向的值不能修改 const NSString * pt2; III 指向常量的常量指针 const NSString *  const pt3; extern 等同于c,全局变量的定义, //

synchronized 修饰在 static方法和非static方法的区别

Java中synchronized用在静态方法和非静态方法上面的区别 在Java中,synchronized是用来表示同步的,我们可以synchronized来修饰一个方法.也可以synchronized来修饰方法里面的一个语句块.那么,在static方法和非static方法前面加synchronized到底有什么不同呢?大家都知道,static的方法属于类方法,它属于这个Class(注意:这里的Class不是指Class的某个具体对象),那么static获取到的锁,是属于类的锁.而非stati