public class Test
{
public readonly string name = "George";
public const string coname = "ABC Company LLC";
public Test(string name)
{
// readonly 修飾的變量能且只能在 Constructor(構(gòu)造函數(shù))中被改變
this.name = name;
}
public string _name
{
get
{
return name;
}
//不可以對readonly修飾的變量進(jìn)行Set操作
//set
//{
// name = value;
//}
}
}
class Program
{
static void Main(string[] args)
{
Test obj = new Test("Test");
//readonly的變量不可以修改值,只能在 Constructor(構(gòu)造函數(shù))中被改變
//obj.name = "New Value";
Console.WriteLine(obj.name);
//const 的變量直接通過對象訪問,不需要實(shí)例化
Console.WriteLine(Test.coname);
Console.Read();
}
}