const、readonly及静态构造的用法
来源:互联网 发布:vs2015写c语言 编辑:程序博客网 时间:2024/06/11 01:02
public class Test{ //const为编译时常量 public const string _cStr = "Const_String"; //readonly为运行时常量 public readonly string _rStr; public static readonly string _srStr; //静态构造不能出现访问修饰符,并且不能够传递任何参数,在程序执行过程中,只会执行一次 static Test() { Console.WriteLine("静态构造,让我先行,程序执行过程中,只调用这唯一的一次!!"); _srStr = "只能在静态构造中赋值(编译时赋值)"; } public Test(string str) { Console.WriteLine("普通构造,可执行多次!!"); _rStr = str; } }public class Tmp{ public Test test; public Tmp(string str) { test = new Test(str); }}class Program{ static void Main(string[] args) { Console.WriteLine(Test._cStr); //Console.WriteLine(Test._srStr); Test test = new Test("ctor_Readonly_String"); Tmp tmp = new Tmp("Test静态构造不调用!"); Console.WriteLine( test._rStr + "," + Test._srStr); Console.ReadKey(); }}
运行图解:
阅读全文
0 0
- const、readonly及静态构造的用法
- const与readonly的用法和区别
- C#--构造函数中readonly与const关键字的不同
- C# static readonly const用法
- const readonly的区别
- C#: const与readonly的用法和区别解析
- C#: const与readonly的用法和区别解析
- readonly与const的区别及类成员初始化方法
- C#中的静态常量(const)和动态常量(static和readonly)用法和区别
- C#中的静态常量(const)和动态常量(static和readonly)用法和区别
- const,readonly和static readonly的区别
- const 和 readonly 的区别
- const 和 readonly 的区别
- readonly 和const的区别
- const 和 readonly 的区别
- readonly 和const的区别
- const 和 readonly 的区别
- const 与readonly的区别
- hdu 6055 Regular polygon
- 深入剖析 iOS 性能优化
- Python 写3D花
- Python程序-2
- HDU4333[Revolving Digits]
- const、readonly及静态构造的用法
- 世界上没有怀才不遇这件事,关键是找准你的优势
- 有重复元素的排列问题
- Linux下动态库(.so)和静态库(.a)
- UVA1608Non-boringSquence
- Listview 里面的按钮点击 复用问题
- JSP上传图片源码
- caffe (11) 其他功能
- java集合框架