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:
// The strName is a form parameter and a variable. Local variables with the same name are not allowed.
Public void SayHelloTo (string strName)
{
Console. WriteLine ("My name is {0}", strName );
}
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:
Static void Main (string [] args)
{
Program pro = new Program ();
Int nNum = 10;
// When the Add method is called, two parameters are input. The first parameter is the actual value,
// The second nNum is a variable, but it has been initialized when the method is passed in.
Int nResult = pro. Add (20, nNum );
}
// Here, a and B are the so-called form parameters.
Public int Add (int a, int B)
{
Return a + B;
}
① 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:
Static void Main (string [] args)
{
Program pro = new Program ();
// Int nNum = 10;
// When the Add method is called, two parameters are input. The first parameter is the actual value,
// The second nNum is a variable, but it has been initialized when the method is passed in.
// Int nResult = pro. Add (20, nNum );
Int I = 10;
Int w = 20;
// Here I * 2 and (w + 10)/10 act as real parameters
Int nResult = pro. Add (I * 2, (w + 10)/10 );
Console. WriteLine (nResult );
}
// Here, a and B are the so-called form parameters.
Public int Add (int a, int B)
{
Return a + B;
}
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.
Class Program
{
Static void Main (string [] args)
{
Program pro = new Program ();
Rectangle rectange = new Rectangle ();
Int myValue = 10;
// Here, the rectange and myValue are real parameters
Pro. CalculateArea (rectange, myValue); // call Method
}
// Here (Rectangle rect, int value) is the form parameter
Public void CalculateArea (Rectangle rect, int value)
{
Rect. Length + = 10;
Rect. width + = 15;
Rect. Area = rect. Length * rect. width;
Value ++;
}
}
// A rectangle class
Public class Rectangle
{
Public int Length = 10;
Public int width = 15;
Public int Area;
}
① 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.