Method Parameters in C# Dr. Neeraj Kumar Pandey
Method Parameters  C# employs four type of Method Parameters 1. Value Parameters 2. Output Parameters 3. Reference Parameters 4. Parameter arrays
Pass by Value using System; class bulb { static void Change(int m) { m=m+10; } public static void Main() { int x=100; Change(x); Console.WriteLine("x="+x); } }
Pass by Reference  A parameter declared with ref keyword is a reference parameter. Example:- void Modify(ref int x) //x is declared as reference parameter  Unlike a value parameter, a reference parameter doesn’t create a new storage location.  When a formal parameter is declared as ref , the corresponding arguments in the method invocation must also be declared as ref. Example:- void Modify( ref int x) { x+=10; //value of m will be changed } int m= 5;//,m is initialized Modify(ref m);//pass by reference
Pass by Reference using System; class bulb { static void Swap(ref int x, ref int y) { int temp=x; x=y; y=temp; } public static void Main() { int m=100; int n=200; Console.WriteLine("Before Swapping:"); Console.WriteLine("m = "+m); Console.WriteLine("n = "+n); Swap(ref m, ref n); Console.WriteLine("After Swapping:"); Console.WriteLine("m = "+m); Console.WriteLine("n = "+n); } }
The Output Parameters  Output parameters are used to pass results back to the calling method.  This is achieved by declaring the parameters with an out keyword.  Similar to reference parameter, out parameter does not create a new storage location. Example:- Void output(out int x) { x=100; } int m; //m is uninitialized output(out m): //value of m is set.
The Output Parameters using System; class bulb { static void Square(int x, out int y) { y=x*x; } public static void Main() { int m;//need not be initialized Square(10, out m); Console.WriteLine("m= "+m); } }
Variable Argument Lists  In C#, we can define methods that can handle variable number of arguments using what are known as PARAMETER ARRAYS.  Parameter arrays are declared using the keyword params. Void function1(params int [ ]x) { }  Here, x has been declared as parameter array. This array must be one dimensional arrays.  A parameter may be a part of a formal parameter list and in such cases, it must be the last parameter.  The method function1 can be invoked in two ways:- • Using int type array as a value parameter. ( function1(a);) • Using zero or more int type arguments for the parameter array.(function (10,20);)
Variable Argument Lists using System; class bulb { static void Parray(params int [ ] arr) { Console.Write("Array elements are:"); foreach(int i in arr) { Console.Write(" "+i); Console.WriteLine(); } } public static void Main() { int [ ] x={11,22,33}; Parray(x); Parray(); Parray(110,220); } } Params is an important keyword in C#. It is used as a parameter which can take the variable number of arguments. Important Point About Params Keyword : • It is useful when programmer don’t have any prior knowledge about the number of parameters to be used. • Only one Params keyword is allowed and no additional Params will be allowed in function declaration after a params keyword. • The length of params will be zero if no arguments will be passed.

Method parameters in c#

  • 1.
    Method Parameters inC# Dr. Neeraj Kumar Pandey
  • 2.
    Method Parameters  C#employs four type of Method Parameters 1. Value Parameters 2. Output Parameters 3. Reference Parameters 4. Parameter arrays
  • 3.
    Pass by Value usingSystem; class bulb { static void Change(int m) { m=m+10; } public static void Main() { int x=100; Change(x); Console.WriteLine("x="+x); } }
  • 4.
    Pass by Reference A parameter declared with ref keyword is a reference parameter. Example:- void Modify(ref int x) //x is declared as reference parameter  Unlike a value parameter, a reference parameter doesn’t create a new storage location.  When a formal parameter is declared as ref , the corresponding arguments in the method invocation must also be declared as ref. Example:- void Modify( ref int x) { x+=10; //value of m will be changed } int m= 5;//,m is initialized Modify(ref m);//pass by reference
  • 5.
    Pass by Reference usingSystem; class bulb { static void Swap(ref int x, ref int y) { int temp=x; x=y; y=temp; } public static void Main() { int m=100; int n=200; Console.WriteLine("Before Swapping:"); Console.WriteLine("m = "+m); Console.WriteLine("n = "+n); Swap(ref m, ref n); Console.WriteLine("After Swapping:"); Console.WriteLine("m = "+m); Console.WriteLine("n = "+n); } }
  • 6.
    The Output Parameters Output parameters are used to pass results back to the calling method.  This is achieved by declaring the parameters with an out keyword.  Similar to reference parameter, out parameter does not create a new storage location. Example:- Void output(out int x) { x=100; } int m; //m is uninitialized output(out m): //value of m is set.
  • 7.
    The Output Parameters usingSystem; class bulb { static void Square(int x, out int y) { y=x*x; } public static void Main() { int m;//need not be initialized Square(10, out m); Console.WriteLine("m= "+m); } }
  • 8.
    Variable Argument Lists In C#, we can define methods that can handle variable number of arguments using what are known as PARAMETER ARRAYS.  Parameter arrays are declared using the keyword params. Void function1(params int [ ]x) { }  Here, x has been declared as parameter array. This array must be one dimensional arrays.  A parameter may be a part of a formal parameter list and in such cases, it must be the last parameter.  The method function1 can be invoked in two ways:- • Using int type array as a value parameter. ( function1(a);) • Using zero or more int type arguments for the parameter array.(function (10,20);)
  • 9.
    Variable Argument Lists usingSystem; class bulb { static void Parray(params int [ ] arr) { Console.Write("Array elements are:"); foreach(int i in arr) { Console.Write(" "+i); Console.WriteLine(); } } public static void Main() { int [ ] x={11,22,33}; Parray(x); Parray(); Parray(110,220); } } Params is an important keyword in C#. It is used as a parameter which can take the variable number of arguments. Important Point About Params Keyword : • It is useful when programmer don’t have any prior knowledge about the number of parameters to be used. • Only one Params keyword is allowed and no additional Params will be allowed in function declaration after a params keyword. • The length of params will be zero if no arguments will be passed.