2014-09-11 66 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