關於C# get set的簡單用法


關於C# get set的文章很多,但是筆者的這篇文章有它的特別之處,筆者用簡單的語言把c# get set講述的十分明了。

C# get set釋一:屬性的訪問器包含與獲取(讀取或計算)或設置(寫)屬性有關的可執行語句。

訪問器聲明可以包含get 訪問器或set 訪問器,或者兩者均包含。聲明采用下列形式之一:get {}set {} get 訪問器  get 訪問器體與方法體相似。它必須返回屬性類型的值。執行 get 訪問器相當於讀取字段的值。以下是返回私有字段 name 的值的 get 訪問器:

  1. private string name;   // the name field  
  2. public string Name   // the Name property  
  3. {     
  4.     get    {      return name;    }  
  5. }   

當引用屬性時,除非該屬性為賦值目標,否則將調用 get 訪問器讀取該屬性的值。例如:Employee e1 = new Employee();...Console.Write(e1.Name);   // The get accessor is invoked here  get 訪問器必須在return或throw 語句中終止,並且控制不能超出訪問器體。set 訪問器set 訪問器與返回 void 的方法類似。它使用稱為 value 的隱式參數,此參數的類型是屬性的類型。

在下例中,set 訪問器被添加到 Name 屬性:

  1.  public string Name  
  2.  {     
  3.      get    {       return name;    }    
  4.      set    {      name = value;    }  
  5. }  

當對屬性賦值時,用提供新值的參數調用 set 訪問器。例如:e1.Name = "Joe";   // The set accessor is invoked here在 set 訪問器中對局部變量聲明使用隱式參數名 (value) 是錯誤的。

C# get set備注:

屬性按如下方式,根據所使用的訪問器進行分類:只帶有get 訪問器的屬性稱為只讀屬性。無法對只讀屬性賦值。只帶有 set 訪問器的屬性稱為只寫屬性。只寫屬性除作為賦值的目標外,無法對其進行引用。同時帶有 get 和 set 訪問器的屬性為讀寫屬性。

在屬性聲明中,get 和set 訪問器都必須在屬性體的內部聲明。使用 get 訪問器更改對象的狀態是一種錯誤的編程樣式。例如,以下訪問器在每次訪問 number 字段時都產生更改對象狀態的副作用。

  1. public int Number   
  2. {     
  3.    get   {      return number++;   // Don't do this   }  
  4. }  

可以將 get 訪問器用於返回字段值,或用於計算字段值並將其返回。例如:

  1. public string Name   
  2.      
  3.    get    {      return name != null ? name : "NA";   }  

在上述代碼段中,如果不對 Name 屬性賦值,它將返回值 NA。示例 1此例說明如何訪問基類中被派生類中具有同一名稱的另一個屬性隱藏的屬性。

  1. // property_hiding.cs  
  2. // Property hidingusing System;  
  3. public class BaseClass   
  4. {    
  5.    private string name;    
  6.    public string Name     
  7.   {       
  8.       get       {         return name;       }    
  9.       set       {         name = value;       }   
  10.   }  
  11. }  
  12. public class DerivedClass : BaseClass   
  13. {     
  14.     private string name;    
  15.     public new string Name   // Notice the use of the new modifier    
  16.    {      
  17.         get       {         return name;       }      
  18.         set       {         name = value;       }    
  19.   }  
  20. }  
  21. public class MainClass   
  22. {  
  23.    public static void Main()    
  24.    {  
  25.      DerivedClass d1 = new DerivedClass();      
  26.      d1.Name = "John"; // Derived class property       
  27.      Console.WriteLine("Name in the derived class is: {0}",d1.Name);      
  28.      ((BaseClass)d1).Name = "Mary"; // Base class property      
  29.      Console.WriteLine("Name in the base class is: {0}",         ((BaseClass)d1).Name);     
  30.   }  
  31. }  

輸出Name in the derived class is: JohnName in the base class is: Mary以下是上例中顯示的重點: 派生類中的屬性 Name 隱藏基類中的屬性 Name。在這種情況下,派生類的該屬性聲明使用 new 修飾符:    public new string Name    {   ...轉換 (BaseClass) 用於訪問基類中的隱藏屬性: ((BaseClass)d1).Name = "Mary";

C# get set釋二: 代碼如下:

  1. public class Car  
  2. {  
  3.     private string color;   
  4.     public string Color   
  5.     {    
  6.           get   {return color;    }    
  7.           set    {color=value;    }  
  8.    }  
  9. }  

我的理解是:通過GET和SET對公有變量Color進行讀寫操作,實際就是間接更改color私有變量的值,那既然如此。為何不設color為public,讓實例直接對color進行讀寫操作呢? 如果有一天,老板讓你把這個類改成當汽車的顏色改變時,同時計算一下汽車的《價格》屬性那么如果直接對Color操作,你不是死定了?  “屬性”是.net的特色之一。

其實就相當於方法,尤其是Java中經常會用到get、set方法(.net的有些思想就是java的)。  屬性的真實作用不只是為了更改某個成員變量的值比如form的size屬性在set的同時要重畫form,如果你不想讓用戶對color修改,就不要提供C# get set方法  是面向對象具有的set and get它的用途:  一般是對類里面的變量進行操作. 而不是直接對類的變量進行操作.

有一個很大的作用就是:  便於維護.因為:如果一個類的一個變量int a ,在其它包或命名空間類中使用了1000次,但是過了許久,你想把a改為b,如果直接對變量a操作的話,就得需求修改整個程序的1000處.  如果用屬性了,就不會了,只需改這個方法即可public int A{ set {   a = value; } get {   return a; }}放為:public int B{ set {   b = value; } get {   return b; }}除去這個屬性之外的地方根本不需要改變。

通過上面的講解。有一點點明白了。是不是讓滿足一定條件讓GET和SET來改變類中的私有變量。而不能讓實例直接操作。像上面的代碼保證了color屬性的安全性。既然如此可不可以寫成set{color=value*20;  //value是不是相當於Color的值} 我當初和你有一樣的想法.但是現在改變了。舉個例子說明一下吧.

  1. public class Car  
  2. {   
  3.    public string Color  
  4.  {     
  5.      get   {      
  6.                    if(this.viewstate["color"]!= null)     
  7.                   {        
  8.                          return this.viewstate["color"];       
  9.                   }      
  10.                         return "":      
  11.             }      
  12.     set    {      this.viewstate["color"];=value;    }   
  13.   }  
  14. }    

在asp.NET中通常這么使用.如果用變量的話就不好使用了。而且C# get set中可以寫多個語句。

原文地址:http://developer.51cto.com/art/200909/151051.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM