Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

C#

Methods
Overview
A method is code, often referred as function in other programming
languages. Every program must have a Main() function to run.

Advantages of using methods are:

- Reusable code.
- Easy to test.
- Modifications to a method do not affect the calling program.
- One method can accept many different inputs.

Declaring a Method
To use a method, you need to declare (i.e. create a function)
the method and then call (i.e. use) it.

Output - To make a method return a value, we must first specify


what data type it returns when declaring the method. If a method
doesn’t need an output, then void is used

Parameters – We can add new variables that we will be able to


input when calling a method.

The following code outputs 0.1, but doesn’t return it

static void Main(string[] args)


{
functionOne(10);
}
static void functionOne(int value)
{
Console.WriteLine(1/value);
}
Optional Arguments
When defining a method, a default value may be set for a
parameter, so that in case if it is not entered, the program
doesn’t crash.
To declare a default value, give a value to the parameter(s):
static void functionTwo(int value1 = 1, int value2 = 2){}

If no value is entered, the program will run with value1 = 1

Named Arguments
When inputting arguments into a method, order of input matters,
to overcome this, it can be specified which arguments the value
goes to:

static void Main(string[] args)


{
functionTwo(value2: 10);
}

The code will assign value2 as 10, and value1 as 1

Passing Arguments
There are three ways to pass arguments to a method when the
method is called: By value, by reference, and as output.

By value:
static void Main(string[] args)
{int a = 3; Sqr(a); Console.WriteLine(a);}
static void Sqr(int x)
{x = x*x;}

The output of this code will still be 3, since Sqr() did not do
anything with a, first of all because it doesn’t return a value,
and also, because to assign a value to a we would write:
a = Sqr(a)
By reference: keyword: ref
static void Main(string[] args)
{int a = 3; Sqr(ref a); Console.WriteLine(a);}
static void Sqr(ref int x)
{x = x*x;}

The output of this code will be 9, because when we use the Sqr()
method, we refer the output value to a, and hence it
automatically assigns it to it.

By output: keyword: out


static void getValues(out int x, out int y)
{x = 12; y = 42}
static void Main(string[] args)
{int a, b = 3; getValues(out a, out b); Console.WriteLine(a
+ ” ” + b);}

The output of this code is 12 42, it means that even though we


assigned value 3 to both a and b, when we output the values from
the method, the values were changed.

Overloading
Method overloading is when multiple methods have same name, but
different parameters.

void Print(int a){Console.WriteLine(a);}


void Print(double a){Console.WriteLine(a);}

This is one example of it. The method Print() would only accept
integer values, if only the first method would be declared. But
with help of second method, now Print() accepts doubles as well.

When overloading methods, the definitions of the methods must


differ from each other by the types and/or number of parameters.

void Print(int a){Console.WriteLine(a);}


void Print(double a){Console.WriteLine(a);}
void Print(int a, int b){Console.WriteLine(a+b);}
You can not overload method declarations that differ only by
return type.
int Func(int a){}
float Func(int a){}
double Func(int a){}

The code above will result in error.

Recursion
Recursion is when a method is being called inside itself. It is
a useful tool that can for example calculate factorials.

static int Fact(int num){


if (num == 1){
return 1;
}
return num * Fact(num – 1);
}

If the input is 1 then the output is 1 as well, if not, the


recursion will eventually bring the value of num to 1, and will
return the calculated number.

You might also like