The difference of static, readonly and const in C#
2010-12-02 16:55
525 查看
readonly和const的初始化后就不能更改, 但是他们还是有区别的。const的初始化是发生在编译的时候,而readonly是发生在运行时,readonly的值可以有两次机会进行初始化:定义的时候和构造函数执行的时候,构造函数执行之后将不能在不更改。
static,可以用来修饰readonly, 结果有点类似于直接使用const(?),但不能修饰 const,static不能修饰类,变量,属性,事件,构造函数和操作符之外的东西。static变量的初始化是发生在变量被调用和静态构造函数被执行之前,具体哪个点MSDN没有明确说明,据说是个不确定的点(有待确认),静态构造函数是在静态类不时候的时候执行的。
static,可以用来修饰readonly, 结果有点类似于直接使用const(?),但不能修饰 const,static不能修饰类,变量,属性,事件,构造函数和操作符之外的东西。static变量的初始化是发生在变量被调用和静态构造函数被执行之前,具体哪个点MSDN没有明确说明,据说是个不确定的点(有待确认),静态构造函数是在静态类不时候的时候执行的。
相关文章推荐
- (C#) What is the difference between "const" and "static readonly" ?
- What is the difference between const and static readonly?(const 和 static readonly 的区别?)
- Question 12: In C++, which of the following is the best declaration for an overloaded operator[] to allow read-only access (and
- The difference between Const and ReadOnly
- Just4Fun - Comparaison between const and readonly in C#
- Just4Fun - Comparaison between const and readonly in C#
- C# const, readonly, static readonly
- C# static readonly const用法
- C# const static readonly
- C#中静态常量(const)和动态常量(static readonly)的一些区别
- const at the end of function and passing const object in a function call
- C# FAQ: const和static readonly有什么区别?
- C# FAQ: const和static readonly有什么区别?
- C#中const与 static readonly 区别(转)
- C#中const与 static readonly 区别
- Usage of readonly and const
- C# const, readonly, static readonly
- the confilict between validation and readonly in jquery
- .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- 设计: ListView 接口,and the missing read-only interfaces in java collection framework