C # Basic Grooming parameters

Source: Internet
Author: User

Basic concepts

Passing data into a method allows the method to have multiple return values.

Passing of parameters

  A value parameter that passes the data by copying the value of the argument to the formal parameter. argument to a value parameter can be a variable or an expression

The following is a simple procedure for value parameter passing

1     class Program2     {3         Static voidMain (string[] args)4         {5Somedata V1 =NewSomedata ();6             intV2 = -;7Console.WriteLine ("V1.value:{0},v2:{1}", V1.value, v2);8 Calculate (v1, v2);9Console.WriteLine ("V1.value:{0},v2:{1}", V1.value, v2);Ten  One         } A  -         Static voidCalculate (Somedata p1,intp2) -         { theConsole.WriteLine ("P1.value:{0},p2:{1}", P1.value, p2); -P1.value + = -; -P2 + = -; -Console.WriteLine ("P1.value:{0},p2:{1}", P1.value, p2); +         } -     } +  A     classSomedata at     { -          Public intValue = -; -}

 

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, and v1 data is stored in the heap (PS: hand-painted picture of the ugly, the picture is also painted ugly, this life and art missed)

2. When invoking a method, V1 is a reference type so only the reference to the V1 is copied to P1,v1 and P1 to the same object in the heap, and V2 is the value type, so the value is copied directly to the P2

3, in the process of method execution, P1 value field and P2 are added 50

4, the method executes after the formal parameter pops from the stack (expands), the final V2 value has not changed, the V1 value has changed. This is also the difference that is often seen in value types and reference types.

  reference parameters , modifier ref, argument must be a variable, and must be assigned before the call. A reference parameter does not create a new space in memory for the formal parameter, it is just an alias for the argument, in memory and the argument points to the same address.

1     class Program2     {3         Static voidMain (string[] args)4         {5Somedata V1 =NewSomedata ();6             intV2 = -;7Console.WriteLine ("V1.value:{0},v2:{1}", V1.value, v2);8Calculate (refV1,refv2);9Console.WriteLine ("V1.value:{0},v2:{1}", V1.value, v2);Ten  One         } A  -         Static voidCalculate (refSomedata P1,ref intp2) -         { theConsole.WriteLine ("P1.value:{0},p2:{1}", P1.value, p2); -P1.value + = -; -P2 + = -; -Console.WriteLine ("P1.value:{0},p2:{1}", P1.value, p2); +         } -     } +  A     classSomedata at     { -          Public intValue = -; -}

  output parameters, modifiers out, arguments must also be variables, and reference parameters are somewhat similar, the parameters of the output parameter is also an alias for the argument, the difference is that the output parameter can not be initialized to the arguments, but in the process of function calls must be assigned to the output parameters, It is not allowed to use formal parameters before they are assigned, and cannot be compiled.

1         Static voidMain (string[] args)2         {3 Somedata v1;4             intv2;5Calculate ( outV1, outv2);6Console.WriteLine ("V1.value:{0},v2:{1}", V1.value, v2);7 8         }9 Ten         Static voidCalculate ( outSomedata P1, out intp2) One         { AP1 =NewSomedata (); -P1.value = -; -P2 = -; theConsole.WriteLine ("P1.value:{0},p2:{1}", P1.value, p2); -         } -     } -  +     classSomedata -     { +          Public intValue = -; A}

A reference type is a value parameter , and if a new object is assigned to a parameter, the association between the formal parameter and the argument is cut off

1         Static voidMain (string[] args)2         {3Somedata V1 =NewSomedata ();4Console.WriteLine ("v1.value:{0}", v1.value);5 Calculate (v1);6Console.WriteLine ("v1.value:{0}", v1.value);7         }8 9         Static voidCalculate (somedata p1)Ten         { OneP1.value = -; AConsole.WriteLine ("p1.value:{0}", p1.value); -P1 =NewSomedata (); -P1.value = -; the  -Console.WriteLine ("p1.value:{0}", p1.value); -         } -     } +  -     classSomedata +     { A          Public intValue = -; at}

If the reference type is used as a reference parameter (with the ref modifier), in the example above, V1 is pointing to data with a value of 50, and does not eliminate the association between the formal parameter and the argument. I'm not drawing anymore here.

C # Basic Grooming parameters

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.