【发布时间】:2014-11-05 05:42:59
【问题描述】:
我想知道为什么对成员的访问应该设置为仅拒绝读取其值。谁能解释一下什么时候使用这些?
提前致谢
【问题讨论】:
标签: c# properties
我想知道为什么对成员的访问应该设置为仅拒绝读取其值。谁能解释一下什么时候使用这些?
提前致谢
【问题讨论】:
标签: c# properties
如果您只想更改成员值,在这种情况下使用只写属性(SET)
例如:
class employee
{
int id = 1;
string name = "chandru";
string dept = "IT";
public string Name
{
get { return name; }
private set { name = value; } //Restricted to modify name to outer this calss
}
public string DEPT
{
set { dept = value; }
}
public void display()
{
Console.WriteLine("ID: {0} Name: {1} and Dept: {2}", id, name, dept);
}
}
class Program
{
static void Main(string[] args)
{
employee e = new employee();
e.DEPT = "CSE";
Console.WriteLine(e.Name);
e.display();
/// e.Name = "Prakash"; //you cannot modify becoz of Access specifies as private
}
}
set 属性仅用于更改您在客户端应用程序中想要的成员值...
注意: 据我所知,我更新了,如果有任何问题,请修改我的答案
【讨论】: