2014-09-11 67 views
0

我想知道为什么访问一个成员应该设置 - 只是否认读取其值。任何人都可以作出解释或何时使用这些的任何例子?在csharp中使用Set-only属性

在此先感谢

如果你想只改变成员的值,在这种情况下,使用只写属性
+0

请参阅http://stackoverflow.com/questions/4695551/write-only-properties-whats-the-point – user1519979 2014-09-11 10:12:08

回答

0

(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 

    } 
} 

集属性用来改变成员值仅是ü希望在客户端应用程序...

注: 我的知识,我更新,请修改我的答案我f什么是错的

+0

嗯,这只是回答“如何”,而不是“何时”或“为什么”。 “这是你想要的客户端应用程序”在我看来是毫无意义的。 – HimBromBeere 2014-09-16 11:13:23