Writing methods in C#

✓ Returning data from method
(a) If a method returns an int type, it can be defined as follow,

int addValues(int leftHandSide, int rightHandSide)
{
    return leftHandSide + rightHandSide;
}

(b) If a method doesn’t want to return anything, return is defined as void,

void showResult(int answer)
{
    System.Console.WriteLine(ans); // display the answer
    return;
}

Calling methods

int result = addValues(39, 3); // on right-hand side of an assignment
showResult(addValues(39, 3)); // as argument to another method call

Applying scope
Local scope – Any variables you declare inside the body of a method are scoped to that method. These variables are called local variables because they are local to the method.

Class scope – Any variables you declare within the body of a class are scoped to that class. The proper C# term for a variable defined by a class is field. It can be used between methods. A method can use a field before the statement that defines the field—the compiler sorts out the details for you.

Overloading methods
Overloading is primarily useful when you need to perform the same operation on different data types or varying groups of information. Although you can overload the parameters of a method, you can’t overload the return type of a method.

public void DoWorkWithData(int intData, float floatData, int moreIntData)
{
    ...
}
public void DoWorkWithData(int intData, float floatData)
{
    ...
}

int arg1 = 99;
float arg2 = 100.0F;
int arg3 = 101;
DoWorkWithData(arg1, arg2, arg3); // Call overload with three parameters
DoWorkWithData(arg1, arg2); // Call overload with two parameters

Optional parameters
A parameter is optional when you define a method by providing a default value for the parameter.

void optMethod(int first, double second = 0.0, string third = “Hello”)
{
    ...
}

In optMethod, the first parameter is mandatory because it does not specify a default value, but the second and third parameters are optional.

Passing named arguments – Named arguments give you the ability to pass arguments in any order. You can rewrite the code that calls the optMethod method, such as shown here:

optMethod(third : “World”, second : 123.45, first : 99);
optMethod(second : 54.321, first : 100);

Suppose that you define the optMethod method as an overloaded method

void optMethod(int first, double second = 0.0, string third = “Hello”)
{
    ...
}
void optMethod(int first, double second = 1.0, string third = “Goodbye”, int fourth = 100 )
{
    ...
}

optMethod(1, 2.5, “World”); // it runs the version that most closely matches themethod call, so it invokes the method that takes three parameters and not the version that takes four. 

optMethod(1, fourth : 101); // it specifies the fourth parameter by name so it invokes 4 parameter method

// the compiler does not let you compile the any of methods.
optMethod(1, 2.5); 
optMethod(1, third : “World”);
optMethod(1);
optMethod(second : 2.5, first : 1);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s