C#參數傳遞和屬性

默認時,值類型是按值傳遞給方法的,也就是說當值對象傳遞方法時,方法中創建對象的一個臨時副本,一旦方法完成,副本被丟棄。

C#提供了ref參數修飾符用於按引用把值對象傳給方法,還有out修飾符用於不經過初始化就傳遞一個ref變量。

public class Time
  {
     // public accessor methods
     public void DisplayCurrentTime()
     {
        System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", 
           Month, Date, Year, Hour, Minute, Second);
     }

     public int GetHour()
     {
        return Hour;
     }

       public void SetTime(int hr, out int min, ref int sec)
       {
           // if the passed in time is >= 30
           // increment the minute and set second to 0
           // otherwise leave both alone
           if (sec >= 30)
           {
               Minute++;
               Second = 0;
           }
           Hour = hr; // set to value passed in

           // pass the minute and second back out
           min = Minute;
           sec = Second;
       }

     // constructor
     public Time(System.DateTime dt)
     {
        
        Year = dt.Year;
        Month = dt.Month;
        Date = dt.Day;
        Hour = dt.Hour;
        Minute = dt.Minute;
        Second = dt.Second;
     }

      // private member variables
     private int Year;
     private int Month;
     private int Date;
     private int Hour;
     private int Minute;
     private int Second;

  }

  public class Tester
  {
     static void Main()
     {
        System.DateTime currentTime = System.DateTime.Now;
        Time t = new Time(currentTime);
        t.DisplayCurrentTime();

           int theHour = 3;
           int theMinute;
           int theSecond = 20;
//out theMinute 便不用初始化
           t.SetTime(theHour, out theMinute, ref theSecond);
           System.Console.WriteLine(
            "the Minute is now: {0} and {1} seconds", 
               theMinute, theSecond);

           theSecond = 40;
           t.SetTime(theHour, out theMinute, ref theSecond);
           System.Console.WriteLine("the Minute is now: {0} and {1} seconds", theMinute, theSecond);

     }

  }

此例說明:theHour作爲值參數傳入,它的全部工作就是設置成員變量Hour,沒有用此參數返回值。

Ref參數theSecond 用於在方法中設置值,使用引用類型時,必須在調用和目兩處指定ref.

theHour,theSecond參數應該必須初始化,theMinute則沒有必要,因爲它是一個out參數,只是爲了返回值。

屬性

屬性結合了字段和方法的多個方面。 對於對象的用戶,屬性顯示爲字段,訪問該屬性需要相同的語法。 對於類的實現者,屬性是一個或兩個代碼塊,表示一個 get 訪問器和/或一個 set 訪問器。 當讀取屬性時,執行 get 訪問器的代碼塊;當向屬性分配一個新值時,執行 set訪問器的代碼塊。 不具有 set 訪問器的屬性被視爲只讀屬性。 不具有 get 訪問器的屬性被視爲只寫屬性。 同時具有這兩個訪問器的屬性是讀寫屬性。

與字段不同,屬性不作爲變量來分類。 因此,不能將屬性作爲 refC# 參考)參數或 outC# 參考)參數傳遞。

屬性具有多種用法:它們可在允許更改前驗證數據;它們可透明地公開某個類上的數據,該類的數據實際上是從其他源(例如數據庫)檢索到的;當數據被更改時,它們可採取行動,例如引發事件或更改其他字段的值。

屬性在類塊中是按以下方式來聲明的:指定字段的訪問級別,接下來指定屬性的類型和名稱,然後跟上聲明 get 訪問器和/或 set 訪問器的代碼塊。 例如:

public class Time
{
   // public accessor methods
   public void DisplayCurrentTime()
   {

      System.Console.WriteLine(
         "Time\t: {0}/{1}/{2} {3}:{4}:{5}",
         month, date, year, hour, minute, second);
   }


   // constructors
   public Time(System.DateTime dt)
   {
      year =      dt.Year;
      month =     dt.Month;
      date =      dt.Day;
      hour =      dt.Hour;
      minute =    dt.Minute;
      second =    dt.Second;
   }

     // create a property
     public int Hour
     {
         get
         {
             return hour;
         }

         set
         {
             hour = value;
         }
     }

    // private member variables
   private int year;
   private int month;
   private int date;
   private int hour;
   private int minute;
   private int second;
}

public class Tester
{
   static void Main()
   {
      System.DateTime currentTime = System.DateTime.Now;
      Time t = new Time(currentTime);
      t.DisplayCurrentTime();

         int theHour = t.Hour;
         System.Console.WriteLine("\nRetrieved the hour: {0}\n",
            theHour);
         theHour++;
         t.Hour = theHour;
         System.Console.WriteLine("Updated the hour: {0}\n", theHour);
   }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章