Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • C# : Understanding pass by reference and pass by value

    • 0
    • 4
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 148
    Comment on it

    Below is the program to demonstrate various method calling conventions followed in C:

    using System;
    class Program{
    	public static void Main(){
    		// ========================= PART I =======================================================
    		int n1 = 9;
    		int n2 = 1;
    		Console.WriteLine( "Before : N1 = {0}, N2 = {1}", n1, n2 );
    		Console.WriteLine("Function called : " + Sum(n1, n2));
    		Console.WriteLine( "After : N1 = {0}, N2 = {1}", n1, n2 );
    		// ========================== PART II ======================================================
    		int sum; // No need to assign it also.......................................................
    		Add(n1, n2 , out sum);
    		Console.WriteLine( "After calling the method: sum = {0}", sum);
    		// ========================== PART III ======================================================
    		int a = 10 , b = 200;//must be assigned before passing them as 'ref'else results in a compile time error....
    		Console.WriteLine( "Before : A = {0}, B = {1}", a, b );
    		Addition(ref a, ref b);
    		Console.WriteLine( "After : A = {0}, B = {1}", a, b );
    	// Sum method demonstrate the use of call by value.............
    	static int Sum(int x, int  y){
    		int ans = x + y, temp;
    		temp = x ;
    		x = y ;
    		y = temp ;
    		return ans;
    	static int Add(int x, int y, out int sum){
    		sum = x + y;
    		return sum;// no need to return also..............
    	// shows the use of 'ref' parameters===================================
    	static void Addition(ref int x, ref int y){

    Hello guys, above program demonstrate the various conventions used by C# for method calling:

    Part 1 : This part shows the basic 'call by value' syntax.

    Part 2 : this part show use of 'out' modifiers , this modifier is useful in the cases where you want to return multiple values from a function. The main point to note about 'out' modifier is that called function needs to assign a value to the out parameter at least once before it returns back. And because of this compiler also lets the user pass an unassigned value to the function as an 'out' parameter. The changes in the out parameter made in the scope of called function reflect back to the  original value,

    Part 3 : this part shows the use of C#'s  "ref" parameter , which passes the reference of the variable to the called function. The basic difference between a ref parameter and an out parameter is that variable passed as 'ref' must be initialized before passing. which is not the case in out modifier.

    Feel free to comment for queries. Hope you found it informative.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: