More about the three keyword params,ref,out in C #

Source: Internet
Author: User
This article mainly discusses the params keyword, the ref keyword, the Out keyword. Very good, with reference value, need to refer to a friend

About these three keywords before you can study the original operation

Using system;using system.collections.generic;using system.text;namespace paramsrefout{  class program  {    static void ChangeValue (int i)    {      i=5;      Console.WriteLine ("The ChangeValue method changed the value" +i.tostring ());    }    static void Main (string[] args)    {      int i = ten;      Console.WriteLine ("The value of I is" +i.tostring ());      ChangeValue (i);      Console.WriteLine ("The value of I is" + i.tostring ());      Console.ReadLine ();}}}  

Observation of running results found

The value is not changed, that is, the operation at this point may be the same as the previous C language function operation

This article mainly discusses the params keyword, the ref keyword, the Out keyword.

1) The params keyword, which is given by the official explanation, is used in cases where the length of the method parameter is variable. Sometimes it is not possible to determine how many method parameters are in a method, and you can use the params keyword to solve the problem.


Using system;using system.collections.generic;using system.text;namespace paramsrefout{  class number  {    public static void Useparams (params int. [] list)    {for      (int i=0;i<list. length;i++)      {        Console.WriteLine (list[i]);      }    }    static void Main (string[] args)    {      useparams (n/a);      int[] MyArray = new Int[3] {10,11,12};      Useparams (MyArray);      Console.ReadLine ();}}}  

2) ref Keyword: Any changes you make to a parameter in a method will be reflected in the variable using the reference type parameter


Using system;using system.collections.generic;using system.text;namespace paramsrefout{  class number  {    static void Main ()    {      int val = 0;      Method (ref val);      Console.WriteLine (Val. ToString ());    }    static void Method (ref int i)    {      i =;}    }  }

3) out keyword: Out is similar to ref but out does not need to be initialized.

Related Article

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.