打印本文 打印本文 关闭窗口 关闭窗口
readonly vs. const [C#]
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2246  更新时间:2009/4/23 10:45:16  文章录入:mintao  责任编辑:mintao

readonly vs. const [C#]

 

Feature:

  • readonly和const都是用来标识常量的。
  • const可用于修饰class的field或者一个局部变量(local variable);而readonly仅仅用于修饰class的field。
  • const常量的值必定在编译时就已明确并且恒定的;而readonly常量却有一点不同,那就是其值可以在运行时编译,当然,它也必须遵守作为常量的约束,那就是值必须恒定不变。
  • const常量必须在声明的同时对其进行赋值,并且确保该值在编译时可确定并恒定;而readonly常量则可以根据情况选择在声明的同时对其赋予一个编译时确定并恒定的值,或者将其值的初始化工作交给实例构造函数(instant constructor)完成。如:public readonly string m_Now = DateTime.Now.ToString();,m_Now会随着运行时实际情况变化而变化。
  • const常量属于类级别(class level)而不是实例对象级别(instant object level),所以它不能跟static结合一起使用,该常量的值将有整个类的所有实例对象共同分享。
  • readonly常量既可以使类级别也可以使实例对象级别的,这取决于它的声明以及初始化工作怎么实施。readonly可以与static结合使用,用于指定该常量属于类级别,并且把初始化工作交由静态构造函数(static constructor)完成。
  • 若readonly常量的初始化工作在声明时就实施而不是交由实例构造函数完成的,并且没有与static结合使用,那么该常量对外行为也会表现得跟类级别常量一样。
  • 能被const修饰声明为常量的类型必须是以下的基元类型(primitive type):sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, float, bool, decimal, string。
  • object, 数组(Array)和结构(struct)不能被声明为const常量。


Example:

using System;

public class
 Order
{
    public Order(string
 id)
    
{
        m_ID =
 id;
    }


    
// 对于每一份订单,其订单序号都是实时确定的常量。
    private readonly string m_ID;
    public string
 ID
    
{
        get { return m_ID; }

    }

    
public override string ToString()
    
{
        return "Order ID: " +
 m_ID;
    }

}
using System;

class
 Customer
{
    public Customer(string name, int
 kind)

[1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口