Methods & Formal Parameters in C#

Methods

When we want to create a method with in the class then it’s important that the method can be declared with access modifier. Declaring a method public makes accessible outside an object whereas declaring it private locks it away inside the object. The syntax of Microsoft Visual C# method is as follows:
[access modifier] returntype methodname(parameterlist)
{
//Method Body
}

In terms of objects and methods, it’s also important to discuss the ‘this’ keyword.‘this’ keyword. This keyword works the same in C# as it does in C++, and it refers to the current object in which your code is executing.

For example, you’re writing code to set up a Color1 that appears in a window and you want to pass the whole button to a method named ColorMe. You can do that by passing this keyword to that method:
public class Color1
{
public void SetUp()
{
ColorMe(this);
}
}

Another use for the ‘this’ keyword is to refer to a field in the current object. In this example, the method SetMessage is passed a parameter named message, which it is supposed to store in the field of the same name in the current object. To avoid confusion, you can refer to the field named message in the current object as this.message, like this:

private string msg;
public SetMessage(string message)
{
this.msg = message;
}

This example also illustrates another use for methods in classes: creating accessormethods. In this case, access to the private msg field is restricted. From outside the current object, you have to call the SetMessage method to assign data to this field. Restricting access to an object’s internal data is usually a wise thing to do, so much so that accessor methods have been formalized in C# into properties.

Formal Parameters

A method’s parameters are the types that get passed to it when the method is called. The list of parameters begins by specifying zero or more ‘fixed parameters’, and it may finish by specifying a single parameter-array.

This latter element – declared using the ‘params’ keyword – means that it is possible to pass an arbitrary number of types to a single method.

Fixed parameter specifications can have either two or three parts (ignoring attributes). The first, optional modifier can be either ‘ref’ or ‘out’. The second part of the specification specifies the parameter’s type, and the third part its name. Examples of these different elements can be seen in the illustrative code in the sections below.

[Modifier] parameter-type parameter-identifier

Parameter Passing

passing by value

The parameter modifiers ‘ref’ and ‘out’ relate to how the parameter is passed into the method. Where neither of these modifiers is used, the parameter is passed in ‘by value’. In this case, when the method is called the value given is copied to the variable specified in the method declaration.

The following example illustrates this point; note that the change made to variable b in the body of the ‘change’ method doesn’t result in a change to the variable a used to invoke the method.
public static void Main()
{
int a = 0;
change(a); // following this method invocation, a equals 0
}

public static void change(int b)
{
b = 5;
}

In this example, it was a value type that was passed ‘by value’. But reference types can also be passed ‘by value’. The immediate value held by a reference type variable is actually a memory address. So when this variable is passed ‘by value’, the memory address is copied to the variable specified in the method head.

But of course, because the two variables will hold the same memory address, any changes made within the method body to the object located at that memory address will be reflected outside the method (although this doesn’t apply for immutable reference types like strings, which act more like value types).

passing by reference

In C# we can pass variables into methods ‘by reference’. Where a variable is passed by reference, the ‘ref’ modifier must be used both in the method head and the method invocation (illustrated by the next code block).

Passing by reference is most obviously useful in cases where we want to treat a value type like a reference type. For example, the method call in the following code does change the value of the variable a passed into the ‘change’ method.
public static void Main()
{
int a = 0;
change(ref a); // following this method invocation, a==5
}

public static void change (ref int b)
{
b = 5;
}

‘output’ parameters

Where a method parameter is defined (and invoked) using the ‘out’ modifier, it is passed by reference. The difference between the ‘out’ and the ‘ref’ modifier is this: a parameter modified by the ‘out’ keyword need not be assigned a value before being passed into the method, but must be assigned a value in the method.

The reason that one might use output parameters is to return multiple values from a method. For instance, in the following code an integer and a boolean is passed to the ‘change’ method. This method sets the boolean to indicate whether or not the integer is greater than 0, and returns the value of the integer doubled.
public static void Main()
{
bool b;
int c = change(5, out b);
}

public static int change (int a, out bool b)
{
b=false;
if (a>0)
b=true;
return (2*a);
}

The ‘params’ modifier

One can pass an arbitrary number of types to a method by declaring a parameter array with the ‘params’ modifier. Note, though, that it is not necessary to place these additional types into an array before calling the method – they can simply be listed in the method invocation (see the next code block for an example).

Types passed as ‘params’ are all passed by value. The following code gives an example of the use of the ‘params’ modifier; the method called ignores the first type passed to it (a double) and returns the sum of all (an arbitrary number of) the integer values passed to it.
public static void Main()
{
double a = 1;
int b = 2;
int c = 3;
int d = totalIgnoreFirst(a, b, c);
}

public static int totalIgnoreF
irst(double a, params int[] intArr)
{
int sum = 0;
for (int i=0; i < intArr.Length; i++)
sum += intArr[i];
return sum;
}