C # Formal parameters, arguments, value passing parameters, reference passing parameters, output parameters, parameter array learning

Source: Internet
Author: User

1) Formal parameters

A formal parameter is, as the name implies, an argument, not an actual parameter, instead of the value of the actual incoming method. The value itself is represented in the method body code to participate in the operation. A formal parameter is defined in a parameter, unlike a local variable in a method body, because it is a variable that does not allow a local variable of the same name in its scope, regardless of whether their type is the same, and does not allow the same name to appear.

Look at the following code example:

1 // One of the strname is that a parameter is also a variable that does not allow local variables of the same name to appear 2          Public void Sayhelloto (string  strName)3        {4             Console.WriteLine ( " My name is {0} " , strName); 5         }

Features of formal parameters:

A ① parameter is a variable that has all the characteristics of a variable. The ② method can have more than one formal parameter, and the parameters are separated directly by commas, even if multiple arguments of the same type cannot be combined.

2) Actual parameters

An argument is an "alias" of the actual value relative to the formal parameter, and the actual value represented by the alias is an argument. The actual value can be a specific value, or it can be a variable

Look at the following code example:

1 Static voidMain (string[] args)2         {3Program Pro =NewProgram ();4             intNnum =Ten;5             //when the Add method is called, two arguments are passed in, and the first one is the actual value,6             //The second nnum is a variable, but has been initialized when the method is passed in7             intNresult = Pro. ADD ( -, nnum);8         }9 Ten         //here A, B is the so-called formal parameter One          Public intADD (intAintb) A         { -             returnA +b; -}

The actual value of the ① argument used for the initial taxiing parameter or the expression ② argument is in the list of method arguments to invoke.

3) passing parameters by value

The value parameter is copied to the formal parameter by the value of the argument. To implement the passing of a value to a method, which is called by value, when the method is called, the CLR does the following:

① allocating space for a parameter in the managed stack heap ② The value of the argument to the formal parameter

Where, in a value parameter, an argument can also be any expression that evaluates to a type requirement, not necessarily a variable.

Look at the following code example:

1 Static voidMain (string[] args)2         {3Program Pro =NewProgram ();4             //int nnum = ten;5             //when the Add method is called, two arguments are passed in, and the first one is the actual value,6             //The second nnum is a variable, but has been initialized when the method is passed in7             //int nresult = Pro. ADD (nnum);8             inti =Ten;9             intW = -;Ten         //Here I * 2 and (W+10)/10 acted as the role of the argument One             intNresult = Pro. ADD (i *2, (W +Ten) /Ten); A Console.WriteLine (nresult); -         } -  the         //here A, B is the so-called formal parameter -          Public intADD (intAintb) -         { -             returnA +b; +}

Here's a piece of code that looks at the parameters, the allocation and usage of the arguments in the managed heap and the managed stack, respectively

1 class Program2     {3         Static voidMain (string[] args)4         {5Program Pro =NewProgram ();6Rectangle Rectange =NewRectangle ();7             intMyValue =Ten;8 9             //The rectange,myvalue here is the actual argument.TenPro. CalculateArea (Rectange, myvalue);//Calling Methods One         } A  -         //here (Rectangle rect,int value) is the formal parameter -          Public voidCalculateArea (Rectangle rect,intvalue) the         { -Rect. Length + =Ten; -Rect.width + = the; -Rect. Area = rect. Length *Rect.width; +value++; -         } +     } A  at     //a rectangular class -      Public classRectangle -     { -          Public intLength =Ten; -          Public intwidth = the; -          Public intArea ; in}

① before a method is called: the system allocates space for the instance rectange and value type myvalue of the rectangle class in the stack, where the reference type Rectange points to the Rectangle object instance in the heap, myvalue is the value type, so its value is in the managed stack. The demo diagram is as follows

② method Invocation starts: The real arguments value is copied to the CalculateArea parameter, where Rectange is a reference type because a new reference--rect is copied, and both references point to the same object at the moment, myvalue is a value type, so you can copy its value directly- Value The demo diagram is as follows:

③ The Length field and width field of the object to which the reference refers, and the value increment by 1, during the method invocation

After the ④ method call, the parameter rect and value are ejected from the stack. MyValue is a value type, and his value does not change (the parameter value is changed); Rectange is a reference type, and the modification to it is actually a modification to the object in the managed heap, whose value is not modified.

To understand the value passing parameters, it is important to understand the function states of value types and reference types in the managed stack and the managed heap. This is very easy to understand.

C # Formal parameters, arguments, value passing parameters, reference passing parameters, output parameters, parameter array learning

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.