C # basic sorting parameters,

Source: Internet
Author: User

C # basic sorting parameters,
Basic Concepts

When data is passed into a method, multiple return values can be returned for the method.

Parameter transfer

Value parameter. Data is transmitted by copying the value of the real parameter to the form parameter. The real parameter of the value parameter can be a variable or expression.

The following is a simple process of passing value parameters.

 1     class Program 2     { 3         static void Main(string[] args) 4         { 5             SomeData v1 = new SomeData(); 6             int v2 = 30; 7             Console.WriteLine("v1.value:{0},v2:{1}", v1.value, v2); 8             Calculate(v1, v2); 9             Console.WriteLine("v1.value:{0},v2:{1}", v1.value, v2);10 11         }12 13         static void Calculate(SomeData p1, int p2)14         {15             Console.WriteLine("p1.value:{0},p2:{1}", p1.value, p2);16             p1.value += 50;17             p2 += 50;18             Console.WriteLine("p1.value:{0},p2:{1}", p1.value, p2);19         }20     }21 22     class SomeData23     {24         public int value = 100;25     }

 

1. Before calling the void Calculate (SomeData p1, int p2) method, the system allocates memory for v1 and v2. v1 references and v2 are stored in the stack, v1 data is stored in the heap (PS: the ugly words in hand-drawn graphs, and the ugly pictures are also painted, which is not the same as art In My Life)

 

2. When the method is called, v1 is a reference type, so only reference v1 is copied to p1. v1 and p1 both point to the same object in the heap; v2 is a value type, so copy the value directly to p2.

3. During method execution, 50 is added to both the value field and p2 of p1.

4. After the method is executed, the form parameter pops up (expanded) from the stack. The value of v2 is not changed, and the value of v1 is changed. This is also the difference between the value type and the reference type.

Reference parameter, modifier ref. The real parameter must be a variable and must be assigned a value before the call. The referenced parameter does not open up new space for the form parameter in the memory. It is just an alias for the real parameter and points to the same address in the memory.

 1     class Program 2     { 3         static void Main(string[] args) 4         { 5             SomeData v1 = new SomeData(); 6             int v2 = 30; 7             Console.WriteLine("v1.value:{0},v2:{1}", v1.value, v2); 8             Calculate(ref v1, ref v2); 9             Console.WriteLine("v1.value:{0},v2:{1}", v1.value, v2);10 11         }12 13         static void Calculate(ref SomeData p1, ref int p2)14         {15             Console.WriteLine("p1.value:{0},p2:{1}", p1.value, p2);16             p1.value += 50;17             p2 += 50;18             Console.WriteLine("p1.value:{0},p2:{1}", p1.value, p2);19         }20     }21 22     class SomeData23     {24         public int value = 100;25     }

The output parameter, modifier out, and real parameter must also be a variable. Similar to the reference parameter, the output parameter is also used as the real parameter alias, the difference is that the output parameter does not need to be initialized for the real parameter, but the output parameter must be assigned a value during the function call process. It is not allowed to use the form parameter before the value is assigned, compilation fails.

 1         static void Main(string[] args) 2         { 3             SomeData v1; 4             int v2; 5             Calculate(out v1, out v2); 6             Console.WriteLine("v1.value:{0},v2:{1}", v1.value, v2); 7  8         } 9 10         static void Calculate(out SomeData p1, out int p2)11         {12             p1 = new SomeData();13             p1.value = 50;14             p2 = 50;15             Console.WriteLine("p1.value:{0},p2:{1}", p1.value, p2);16         }17     }18 19     class SomeData20     {21         public int value = 100;22     }

 

The reference type is used as the value parameter. If a new object is assigned to the shape parameter, the association between the shape parameter and the real parameter is cut off.

 1         static void Main(string[] args) 2         { 3             SomeData v1 = new SomeData(); 4             Console.WriteLine("v1.value:{0}", v1.value); 5             Calculate(v1); 6             Console.WriteLine("v1.value:{0}", v1.value); 7         } 8  9         static void Calculate(SomeData p1)10         {11             p1.value = 60;12             Console.WriteLine("p1.value:{0}", p1.value);13             p1 = new SomeData();14             p1.value = 50;15 16             Console.WriteLine("p1.value:{0}", p1.value);17         }18     }19 20     class SomeData21     {22         public int value = 100;23     }

 

 

If the reference type is used as the reference parameter (with the ref modifier), v1 points to the data with a value of 50 in the preceding example, and does not eliminate the association between the form parameter and the real parameter. I will not draw any picture here.

 

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.