C#参数传递和属性

来源:互联网 发布:网络防御模型ppdr 编辑:程序博客网 时间:2024/06/05 11:23

默认时,值类型是按值传递给方法的,也就是说当值对象传递方法时,方法中创建对象的一个临时副本,一旦方法完成,副本被丢弃。

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);   }}


0 0
原创粉丝点击