Const与Readonly

2023-06-20,,

Const常量,必须给它初始化值。并且不能改变它。

比如 public Const double Pi=3.1315926;

用于不需要改变值,固定结果。

 

ReadOnly只读,用于不确定给定的值,需要计算后才能确定。

比如:我有个背景颜色;需要判断今天是周几后给定值。

public class getColor

{

     public static readonly string color;

     static  getColor{ if(Datetime.DayOfWeek==DayOfWeek.wednesDay){ color="yellow"; }  }

 

    getColor(){ HttpContent.current.Response.Write(color);}

}

 

初始化输出颜色。

 

《Const与Readonly.doc》

下载本文的Word格式文档,以方便收藏与打印。