A quick synopsis on the differences between 'const' and 'readonly' in C#:
'const':
- Can't be static.
- Value is evaluated at compile time.
- Initiailized at declaration only.
'readonly':
- Can be either instance-level or static.
- Value is evaluated at run time.
- Can be initialized in declaration or by code in the constructor.
You can't use the static keyword with a const because it would be redundant. You can think of const as a subset of static. Consts are static, not instance members.
---------------------------------------------------------------------------------------------------------------------
C# const and readonly
C# has two different keywords for defining fields whose values wont change (const and readonly)
At the outset both of them might look similar but if we dig a little deep, there are quite a number of differences.
The following are the differences between C# const and readonly
const |
readonly |
Evaluated at compile time |
Evaluated at runtime |
Implicitly static |
Can be static or instance |
Initialized at the place where it is declared |
Can be initialized in the constructor |
Once value is changed, the change is not reflected in the client code and needs to be recompiled for the change to be seen |
Value of the readonly field is fetched dynamically , so recompile of the client code is not necessary. |
Only primitive types can be declared as consts |
User defined types also can be declared as readonly |
It is always safer to use readonly as they are versioning friendly and constants are a disaster if you have to change the value of the value later. If you are in doubt whether to use const or readonly use readonly
--------------------------------------------------------------------------------------------------------------------------------
const 和 readonly 的区别,总是不太清楚,于是查了查资料。
const 的概念就是一个包含不能修改的值的变量。
常数表达式是在编译时可被完全计算的表达式。因此不能从一个变量中提取的值来初始化常量。
如果 const int a = b+1;b是一个变量,显然不能再编译时就计算出结果,所以常量是不可以用变量来初始化的。
readonly 允许把一个字段设置成常量,但可以执行一些运算,可以确定它的初始值。
因为 readonly 是在计算时执行的,当然它可以用某些变量初始化。
readonly 是实例成员,所以不同的实例可以有不同的常量值,这使readonly更灵活。
readonly 关键字与 const 关键字不同。
1. const 字段只能在该字段的声明中初始化。
readonly 字段可以在声明或构造函数中初始化。因此,根据所使用的构造函数,readonly 字段可能具有不同的值。
2. const 字段是编译时常数,而 readonly 字段可用于运行时常数。
3. const 默认就是静态的,而 readonly 如果设置成静态的就必须显示声明。
4.const 对于引用类型的常数,可能的值只能是 string 和 null。
readonly可以是任何类型
* 需要注意的一个问题是:
对于一个 readonly 的 Reference 类型,只是被限定不能进行赋值(写)操作而已。而对其成员的读写仍然是不受限制的。
public static readonly Class1 my = new Class1();
…
my.SomeProperty = 10; //正常
my = new Class1(); //出错,该对象是只读的
但是,如果上例中的 Class1 不是一个 Class 而是一个 struct,那么后面的两个语句就都会出错。
static readonly:
Java 中 static 是当载入一个类时执行一次的。
C#中是怎么执行的,我没有查到。很奇怪几乎每本java的书都会说static的问题,C#的往往只说怎么用,但是应该是在main函数调用之前初始化,所以static readonly也是运行时的,可以用变量付值,如:
private static readonly string path = System.Windows.Forms.Application.StartupPath + “aaa”;
-----------------------------------------------------------------------------------------------------------------------------------------
static 修饰的变量意味着它属于类级别,不需要实例化就可以直接通过 类名.变量名 来用。 下面是一个演示示例: using System; namespace ConsoleApplication1 Program(String tt) public class myClass static void Main(string[] args) |