public class Test
{
public readonly string name = "George";
public const string coname = "ABC Company LLC";
public Test(string name)
{
// readonly 修饰的变量能且只能在 Constructor(构造函数)中被改变
this.name = name;
}
public string _name
{
get
{
return name;
}
//不可以对readonly修饰的变量进行Set操作
//set
//{
// name = value;
//}
}
}
class Program
{
static void Main(string[] args)
{
Test obj = new Test("Test");
//readonly的变量不可以修改值,只能在 Constructor(构造函数)中被改变
//obj.name = "New Value";
Console.WriteLine(obj.name);
//const 的变量直接通过对象访问,不需要实例化
Console.WriteLine(Test.coname);
Console.Read();
}
}