神刀安全网

Value parameters vs Reference parameters vs Output Parameters in C#

Value Parameters in C# :

When a method is called,the system does the following:

  • It allocates space on the stack for the formal parameters.
  • It copies the values of the actual parameters to the formal parameters.

An actual parameter for a value parameter doesn’t have to be a variable. It can be any expression evaluating to the matching data type.When you use value parameters, data is passed to the method by copying the value of the actual parameter to the formal parameter.

Let’s look at below example to understand it better.In below example, a method called MyMethod, which takes two parameters—a variable of type MyClass and an int.– The method adds 5 to both the int type field belonging to the class and to the int

 class MyClass     {         public int Val = 20; // Initialize the field to 20.     }   class Program   //Formal parameters   {       static void MyMethod(MyClassf1, int f2)       {           f1.Val = f1.Val + 5; // Add 5 to field of f1 param.           f2 = f2 + 5; // Add 5 to second param.           Console.WriteLine("f1.Val: {0}, f2: {1}", f1.Val, f2);       }       static void Main()       {           MyClassa1 = new MyClass();           int a2 = 10;//Actual parameters           MyMethod(a1, a2); // Call the method.           Console.WriteLine("f1.Val: {0}, f2: {1}", a1.Val, a2);       }   } 

Output:

f1.Val: 25, f2: 15

f1.Val: 25, f2: 10

Let’s discuss the values of the actual and formal parameters at various stages in the execution of the method:

Value parameters vs Reference parameters vs Output Parameters in C#

  • Before method execution, variables a1 and a2 are already in stack and will be used as actual parameter
  • In beginning of the method,the system will have allocated space on the stack for the formal parameters and copied the values from the actual parameters.
    − a1 is a reference type,so the reference is copied,resulting in both the actual and formal parameters referring to the same object in the heap.
    − a2 is a value type, the value is copied, producing an independent data item.
  • At the end of the method, both f2 and the field of object f1 have been incremented by 5.
  • After method execution,the formal parameters are popped off the stack.
    − a2 is value type,so the value is unaffected by the activity in the method.
    − a1 is reference type,so the value has been changed by the activity in the method.

Reference Parameters in C#:

You must use a ref modifier in both the method declaration and the invocation.They do not allocate memory on the stack for the formal parameters.Instead,a formal parameter name acts as an alias for the actual parameter variable, referring to the same memory location.

 class MyClass     {         public int Val = 20; // Initialize field to 20.     }     class Program     {         static void MyMethod(refMyClassf1, refint f2)         {             f1.Val = f1.Val + 5; // Add 5 to field of f1 param.             f2 = f2 + 5; // Add 5 to second param.             Console.WriteLine("f1.Val: {0}, f2: {1}", f1.Val, f2);         }         static void Main()         {             MyClassa1 = new MyClass();             int a2 = 10;             MyMethod(refa1, refa2); // Call the method.             Console.WriteLine("f1.Val: {0}, f2: {1}", a1.Val, a2);         }     } 

Output:

f1.Val: 25, f2: 15f1.Val: 25, f2: 15

Value parameters vs Reference parameters vs Output Parameters in C#

  • Before method execution, variables a1 and a2 are already in stack and will be used as actual parameter
  • In beginning of the method,the names of the formal parameters will have been set as aliases for the actual parameters
    − variables a1 and f1 refered to same location and a2 and f2 refered to same location
  • At the end of the method, both f2 and the field of the object of f1 have been incremented by 5.
  • After method execution,the names of the formal parameters are gone,but both the value of a2, which is the value type, and the value of the
    object pointed at by a1, which is the reference type, have been changed by the execution in the method

Output Parameter:

Output parameters are used to pass data from the method back out to the calling code.You must use a Out modifier in both the method declaration and the invocation.The actual parameter must be a variable—it cannot be another type of expression.

Let’s look at below example to understand it better.

 class MyClass     {         public int Val = 20; // Initialize field to 20.     }     class Program     {         static void MyMethod(outMyClassf1, outint f2)         {             f1 = new MyClass(); // Create an object of the class.             f1.Val = 25; // Assign to the class field.             f2 = 15; // Assign to the int param.         }         static void Main()         {             MyClassa1 = null;             int a2;             MyMethod(outa1, outa2); // Call the method.         }     } 

Value parameters vs Reference parameters vs Output Parameters in C#

  • Before method execution, variables a1 and a2 are already in stack and will be used as actual parameter
  • In beginning of the method,the names of the formal parameters will have been set as aliases for the actual parameters
    − variables a1 and f1 refered to same location and a2 and f2 refered to same location
  • Inside the method,the code creates an object of type MyClass and assigns it to f1.It then assigns a value to f1’s field and also assigns a value to f2. The assignments to f1 and f2 are both required, since they are output parameters.
  • After method execution,the names of the formal parameters are gone,but both the value of a2, which is the value type, and the value of the
    object pointed at by a1, which is the reference type, have been changed by the execution in the method

Thanks for visiting !!

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Value parameters vs Reference parameters vs Output Parameters in C#

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址