Out,ref parameters

Source: Internet
Author: User

class Method      {           // 值参数(Value Parameter):方法名称(参数类型 参数名称 [,参数类型 参数名称])           // 引用参数(Reference Parameter):方法名称(ref 参数类型 参数名称 [,ref 参数类型 参数名称])           // 输出参数(Output Parameter):方法名称(out 参数类型 参数名称 [,out 参数类型 参数名称])          // 字符串在创建后就不能改变,但我们对其进行操作改变时,我们实际上操作的是它的一个副本,若要直接操纵它可以使用ref或out关键字          // string和数组都是引用类型的变量          // 值参数          public static void ValueMethod( int i)          {              i++;          }          // 引用参数(ref):ref在使用前需要先初始化变量的值,在方法体外先初始化。          public static void RefMethod( ref int i)          {              i++;          }          // 输出参数(out):out参数必须在方法体内初始化          public static void OutMethod( out int i)          {              i = 0;              i++;          }                   // 可变参数params, params后面必须跟数组,之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字          static int addi( params int [] values)          {              int sum = 0;              foreach ( int i in values)              {                  sum = sum + i;              }              return sum;          }          // 主函数          static void Main()          {              // 值参数              int i = 0; // 这里声明变量 i ,那么在内存堆栈中就为i开了一个新的空间并存放它              ValueMethod(i); // 执行函数后,ValueMethod中的 i 被加1,这里的参数 i 只是上面声明的变量 i  的一个拷贝,执行后,原始变量 i 没有改变                           Console.WriteLine( "i=" + i); // 结果:i=0,说明这里的 i 和ValueMethod()函数里的 i 不是同一个 i              //============================================================              // 引用参数ref              int j = 0; // ref 在使用前需要初始化变量的值。              RefMethod( ref j); // ★ ref 相当于声明了一个指针,直接指向变量j,并操纵它,内存中j只有一个,未发生拷贝              Console.WriteLine( "j=" + j); // 结果:j=1,说明第一个j也变成了1,即内存中最原先的变量j的值变成了1              //============================================================              // 输出参数out              int k; // 在这里不能对k初始化,如int k=0是不行的              OutMethod( out k); // ★ out 和引用参数类型ref一样,操作的也只有一块内存地址              Console.WriteLine( "k=" + k); // 结果:k=1,这里说明用ref参数和用out参数得到的结果是一样的              //★ ref和out雷同,主要的区别在于谁负责初始化              //  ref 参数必须在使用前先初始化,也就是在方法体外初始化              //  out 参数必须在方法体内初始化              //==============================================================              // 可变参数              // 一般做法是先构造一个对象数组,然后将此数组作为方法的参数              int [] arr = new int [3] { 1, 2, 3 };              Console.WriteLine(addi(arr));              // 而使用了params修饰方法参数后,我们可以直接使用一组对象作为参数              Console.WriteLine(addi(1, 2, 3));              Console.ReadKey();          }      }public partial class Form1:form     {        public Form1 ()         {          & nbsp InitializeComponent ();       }         private void Form1_Load (object Sende R, EventArgs e)         {           //A,B,C for testout initial preparatory work             String a = "lid";            string b = "Room";      & nbsp     String c=string. empty;           //At the time of the call, the output parameter C should be specifically added out, otherwise error yo             Testo UT (a,b,out c);           //After running the Testout method, the value of the output parameter C can be called elsewhere         &NBSP ;   Label4. Text = c;                   }    &NBsp  //Declare a method with output out parameters, note the syntax out data type method parameters         void Testout (string x, String y, out string z) &nbs P       {            z = x + y;       }   }

Out,ref Parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.