const和readonly的值一旦初始化则都不再可以改写;
const只能在声明时初始化;readonly既可以在声明时初始化也可以在构造器中初始化;
const隐含static,不可以再写static const;readonly则不默认static,如需要可以写static readonly;
const是编译期静态解析的常量(因此其表达式必须在编译时就可以求值);readonly则是运行期动态解析的常量;
const既可用来修饰类中的成员,也可修饰函数体内的局部变量;readonly只可以用于修饰类中的成员.
做成属性,
private string m_str=null;
public string str
{
get
{
return m_str;
}
set
{
if(m_str==null)
{
m_str=value;
}
}
}
声明成静态类型变量 static final int pi=3.14;
或者常量 const int pi =3.14;
或者只读变量 readonly int pi=3.14;
具体选择看你程序的应用了!
定义为常量 并初始化
湖北新蓝海是一家专注于网络营销 网络推广的领头企业 ,已为武汉健民、华工激光、江西仁和等多家知名企业提供网络营销外包服务。详情请到各大搜索引擎中搜索“湖北新蓝海”
private string xx = null;
public string MyProperty
{
get { return xx; }
set
{
if (xx == null)
{
xx = value;
}
else
{
throw new Exception();
}
}
}