C # parameters, real parameters, value passing parameters, reference passing parameters, output parameters, learning of parameter arrays (1)

Source: Internet
Author: User

1) Parameters

As the name implies, a formal parameter is not an actual parameter. It replaces the value of the actual input method. In the method body code, the value itself is involved in the operation. The parameter is defined in parameters. It is different from the local variables in the method body. Because it is a variable, a local variable with the same name is not allowed in its scope, regardless of whether their types are the same, the same name is not allowed.

See the following code example:

1 // The strName is a form parameter and is also a variable. Local variables with the same name are not allowed. 2 public void SayHelloTo (string strName) 3 {4 Console. writeLine ("My name is {0}", strName); 5}

Parameters:

① A parameter is a variable, which has all the characteristics of the variable. ② There can be multiple form parameters in the method, and the form parameters are separated by commas. Even multiple parameters of the same type cannot be declared together.

2) real parameters

The real parameter is relative to the actual parameter, and the actual value represents the real parameter. The actual value can be a specific value or a variable.

See the following code example:

1 static void Main (string [] args) 2 {3 Program pro = new Program (); 4 int nNum = 10; 5 // when the Add method is called, two parameters are passed in. The first one is the actual value, and the sixth one is the variable. However, 7 int nResult = pro has been initialized when the method is passed in. add (20, nNum); 8} 9 10 // here a, B is the so-called shape parameter 11 public int Add (int a, int B) 12 {13 return a + B; 14}

① Real parameters are used to initialize the actual values or expressions of the form parameters ② real parameters are in the list of method parameters to be called.

3) PASS Parameters by value

The value parameter is copied to the form parameter by copying the value of the real parameter. To pass the value to the method, that is, pass by value. When the method is called, CLR performs the following operations:

① Allocate space for the form parameter in the managed stack heap ② copy the value of the real parameter to the form parameter

In the value parameter, the real parameter can also be any expression that meets the type requirements of the calculation result, not necessarily a variable.

See the following code example:

1 static void Main (string [] args) 2 {3 Program pro = new Program (); 4 // int nNum = 10; 5 // when the Add method is called, input two parameters, the first is the actual value, 6 // The second nNum is the variable, but the method has been initialized 7 // int nResult = pro. add (20, nNum); 8 int I = 10; 9 int w = 20; 10 // here I * 2 and (w + 10) /10 acts as the real parameter. 11 int nResult = pro. add (I * 2, (w + 10)/10); 12 Console. writeLine (nResult); 13} 14 15 // here a, B is the so-called shape parameter 16 public int Add (int a, int B) 17 {18 return a + B; 19}

Next, let's take a look at the code and observe the distribution and usage of the parameters in the managed heap and managed stack respectively.

1 class Program 2 {3 static void Main (string [] args) 4 {5 Program pro = new Program (); 6 Rectangle rectange = new Rectangle (); 7 int myValue = 10; 8 9 // rectange here, myValue is the real parameter 10 pro. calculateArea (rectange, myValue); // call method 11} 12 13 // here (Rectangle rect, int value) is the form parameter 14 public void CalculateArea (Rectangle rect, int value) 15 {16 rect. length + = 10; 17 rect. width + = 15; 18 rect. area = rect. length * rect. width; 19 value ++; 20} 21} 22 23 // a rectangle class 24 public class Rectangle25 {26 public int Length = 10; 27 public int width = 15; 28 public int Area; 29}

① Before a method is called: The system allocates space for the rectange and Value Type myValue of the Rectangle class instance in the stack. The reference type rectange points to the Rectangle object instance in the heap, and myValue is the value type, therefore, the value is in the managed stack. The demo is as follows:

② At the beginning of method call: The real parameter copies the value to the shape parameter of CalculateArea, where rectange is the reference type, because copying a new reference-rect, both references point to the same object at the moment, myValue is a value type, so you can directly copy its value -- value. The demo is as follows:

③ When the method is called, the length and width fields of the object to which the reference is referenced are changed, and the value is increased by 1.

 

④ After the method is called, the form parameters rect and value are popped up from the stack. MyValue is a value type, and its value is not changed (the parameter value is changed); rectange is a reference type, and the modification to it is actually a modification to the objects in the managed heap, the value that has not been modified.

 

To understand the value transfer parameters, we mainly understand the role status of value types and reference types in managed stacks and managed stacks. This is easy to understand.

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.