Value parameters and Reference parameters

Source: Internet
Author: User

When the value parameter method is invoked, the system does the following: 1 . Allocate space for the parameters in the stack; 2 . Copy the value of the argument to the parameter, and before the variable is used as the argument, the variable must be assigned (unless it is an output parameter), and for a reference type, the variable can be set to an actual reference or null. 
namespaceconsoleapplication1{classA { Public intVal=Ten; }    classProgram {Static voidMethod (A Obj2,intF1) {Obj2.val= Obj2.val +5; F1= F1 +3; Console.WriteLine ("{0} {1}", OBJ2.VAL,F1); }        Static voidMain (string[] args) {A obj=NewA (); intt = -;            Method (Obj,t); Console.WriteLine ("{0} {1}", Obj.val, T);        Console.ReadLine (); }    }}



Reference parameters
When you use reference parameters, you must use the ref modifier on both the declaration and the invocation of the method. The argument must be a variable and must be assigned before it is used as an argument. If it is a reference type variable, it can be assigned a reference or null, and for a value parameter the system allocates memory for the parameters on the stack, instead, the reference parameter has the following characteristics:
1. No memory is allocated on the stack for the formal parameter;
2. The parameter name of the formal parameter is the alias of the argument variable, pointing to the same memory location.
classProgram {Static voidMethod ref AObj2, ref int F1) {Obj2.val= Obj2.val +5; F1= F1 +3; Console.WriteLine ("{0} {1}", OBJ2.VAL,F1); }        Static voidMain (string[] args) {A obj=NewA (); intt = -; Method (  ref obj,ref  t); Console.WriteLine ("{0} {1}", Obj.val, T);        Console.ReadLine (); }    }


 reference type as a value parameter 
for a reference type object, whether passing it as a value parameter or a reference parameter, we can modify his member value inside the method.
Namespace consoleapplication1{class A {public int val=10; } class Program {static void method (A obj2) {obj2.val = 5; Console.WriteLine ("{0}", Obj2.val); Obj2 = new A (); Console.WriteLine ("{0}", Obj2.val); } static void Main (string[] args) {A obj = new A (); Console.WriteLine ("{0}", Obj.val); Method (obj); Console.WriteLine ("{0}", Obj.val); Console.ReadLine (); } }}
The object of the reference type is passed as a value parameter, and if a new object is assigned to the parameter inside the method, the connection between the formal parameter and the argument is cut off, and the new object will no longer exist after the method finishes.

  Reference type as reference parameter

namespaceconsoleapplication1{classA { Public intVal=Ten; }    classProgram {Static voidMethodrefA Obj2) {Obj2.val=5; Console.WriteLine ("{0}", Obj2.val); Obj2=NewA ();//The memory parameters and arguments allocated at this time point to this piece of memory, which will not be freed even after the method is executed, and obj points to this memoryConsole.WriteLine ("{0}", Obj2.val); }        Static voidMain (string[] args) {A obj=NewA ();//static void Method (ref A Obj2) {...} This memory will be automatically collected by the garbage collector when the method finishes executingConsole.WriteLine ("{0}", Obj.val); Method (refobj); Console.WriteLine ("{0}", Obj.val);        Console.ReadLine (); }    }}

Value parameters and Reference 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.