using System;
public class ReadOnlyClass
{
private readonly int IntegerConstant;
public ReadOnlyClass ()
{
IntegerConstant = 5;
}
// We get a compile time error if we try to set the value of the readonly
//class variable outside of the constructor
public int IntMember
{
set
{
IntegerConstant = value;
}
get
{
return IntegerConstant;
}
}
public static void Main(string[] args)
{
ReadOnlyClass obj= new ReadOnlyClass();
// We cannot perform this operation on a readonly field
obj.IntMember = 100;
Console.WriteLine("Value of IntegerConstant field is {0}",
obj.IntMember);
}
} 作者: 雷向阳 时间: 2012-10-10 23:15
private:私有成员, 在类的内部才可以访问。
protected:保护成员,该类内部和继承类中可以访问。
public: 公共成员,完全公开,没有访问限制。
internal:在同一命名空间内可以访问