Method Parameters in C#


Introduction:

In this article let us see the structure of method. There are four kinds of formal parameters:

  • Value parameters, which are declared without any modifiers.
  • Reference parameters, which are declared with the ref modifier.
  • Output parameters, which are declared with the out modifier.
  • Parameter arrays, which are declared with the params modifier.

Value parameters:

Value parameter is also called In parameter. A parameter declared with no modifiers is a value parameter. A value parameter corresponds to a local variable that gets its initial value from the corresponding argument supplied in the method invocation. A method is permitted to assign new values to a value parameter. Such assignments only affect the local storage location represented by the value parameter-they have no effect on the actual argument given in the method invocation.

Reference parameters:

A parameter declared with a ref modifier is a reference parameter. Contrastingly to value parameter, a reference parameter does not make a new storage location. Instead, a reference parameter represents the same storage location as the variable given as the argument in the method invocation. Within a method, a reference parameter is always considered definitely assigned.

Output parameters:

A parameter declared with an out modifier is an output parameter. Similar to a reference parameter, an output parameter does not create a new storage location. Instead, an output parameter represents the same storage location as the variable given as the argument in the method invocation. Every output parameter of a method must be definitely assigned before the method returns. Output parameters are typically used in methods that produce multiple return values.

Parameter arrays:

A parameter declared with a params modifier is a parameter array. If a formal parameter list includes a parameter array, it must be the last parameter in the list and it must be of a single-dimensional array type. For example, the types string[] and string[][] can be used as the type of a parameter array, but the type string[,] can not. It is not possible to combine the params modifier with the ref and out modifiers.

Let us see all the above in an example.

using system;
class Data
{
public string name;
public string qualification;
}
class paramet
{
public static void Main()
{
string choice;
paramet p =
new paramet();
do {
choice = p.getchoice();
choice = p.makeconclusion(choice);
Console.Write("Press any key to continue");
Console.ReadLine();
Console.WriteLine();
}
while (choice != "Q" && choice != "q");
}
// To show Menu and get users choice.
string getchoice()
{
string choice;
Console.WriteLine("A -- Add name");
Console.WriteLine("R -- Refresh name");
Console.WriteLine("U -- Update name");
Console.WriteLine("N -- Notice name");
Console.WriteLine("Q -- Quit\n");
Console.WriteLine("Choice (A,R,U,N,or Q):");
choice = Console.ReadLine();
return choice;
}
//Make Conclusion
void makeconclusion(string choice)
Data gg =
new Data();
switch(choice)
{
case"A":
case"a":
gg.name="G.G.SARADHA";
gg.qualification="B.E";
this.addData(ref gg);
break;
case"R":
case"r":
gg.name="G.N.VADIVAMBAL";
this.refreshData(gg.name);
break;
case"U":
case"u":
gg.name="G.A.GNANAVEL";
this.updateData(out gg);
Console.WriteLine(" Name is now {0}.",gg.name);
break;
case"N":
case"n":
this.noticeData("G.G.SARADHA","G.A.GNANAVEL","G.N.VADIVAMBAL");
break;
case"Q":
case"q":
Console.WriteLine("THANK YOU");
break;
default:
Console.WriteLine("{0} is not a Valid choice",choice) ;
}
}
void addData(ref Data gg)
{
Console.WriteLine("The Name :{0},The qualification: {1} added.",gg.name,gg.qualification);
}
void refreshData(string name)
{
Console.WriteLine("The Name you wish to refresh:{0}.",name);
}
void updateData(out Data gg)
{
//Console.WriteLine("The Name :{0}",gg.name);//causes Error.
gg = new Data();
gg.name = "ARUNGG";
gg.qualification = "B.E";
}
void noticeData(params string[] names)
{
foreach (string name in names)
{
Console.WriteLine("The Name is:{0}.",name);
}
}
}

Program Description:

In the above example I use that 4 types (out, ref, params, value) of parameters.
To illustrate the usage of parameters, I created a Data class with two strings fields.

In Main() I call getchoice() to get the user's input and put that string in the choice variable. Then I use the choice as an argument to makeconclusion(). In the declaration of makeconclusion() You will notice it's one parameter is declared as a string with the name choice. Again, this is a new choice, separate from the caller's argument and local only to this method. Since makeconclusion()'s choice parameter does not have any other modifiers, it is considered a "value" parameter. The actual value of the argument is copy on the stack. Value parameters are input only.

The Switch statement in makeconclusion() calls a method for each case. These method calls are different from one I used in Main().Instead of using the "p" reference ,I use the "this" keyword. "this" is a reference to the current object.

The addData method takes a "ref" parameter. This means the parameter is passed as a reference. The reference is copied on the stack when it is passed to the method. This reference still refers to the same object on the heap as the original reference used in the caller's argument. This means any changes to the local refrence's object also change the caller reference's object.

The updateData() has an "out" parameter. "out" parameters are only passed back to the calling function.

"params" is a very useful addition to the C# language. In makeconclusion() I pass in three comma delimited string arguments. The number of arguments is variable.In noticeData() I use a foreach loop to print each of these strings. The "params" parameter is considered an inputonly parameter and any changes affect the local copy only. That's all about method parameters.

Conclusion:

In summary, I believe that user's understand some useful items such as method patameters(Value parameters,Reference parameters,Output parameters,Parameter arrays,foreach loop etc.) from this article.


Similar Articles