4. Methods
Methods are used to define logic once and use it at many places, which make
maintenance of program easier.
Syntax of declaring methods”
[Attributes]
access-modifiers return-type methodname( parameters )
{
methodbody
}
5. Methods
Access modifiers can be private, public, protected, internal, protected internal.
- Return type can be any valid date type or void if nothing is returned by method.
- Method name is name of the method.
- Parameters can be any data type. Parameters are optional.
Methods are of two types:
- Static Methods
- Instance methods
6. Static methods
Static Methods are associated with class. Example:
class Program
{
//declaring and using static method
public static void print()
{
Console.WriteLine("printing done");
}
static void Main(string[] args)
{
print();
Console.ReadKey();
}
}
7. Instance methods
Instance methods are associated with instance. Example:
class Program
{
//declaring and using instance method
public void print()
{
Console.WriteLine("printing done");
}
static void Main(string[] args)
{
Program p = new Program();
p.print();
Console.ReadKey();
}
}
8. Methods parameters
There are four types of method parameters:
- Value parameters
Passing parameter by value
- Reference parameters
Passing parameter by reference
- Out parameters
Out parameter is used when we want multiple values output from single method and
don’t know the initialization value of input parameter.
- Parameters arrays
Passing array as parameter
9. Value parameters
Passing parameter by value:
class Program
{
//passing parameter by value
public void increment(int i)
{
++i;
}
static void Main(string[] args)
{
Program p = new Program();
int j = 10;
p.increment(j);
Console.WriteLine(j);
Console.ReadKey();
}
}
Result :-
10
10. Reference parameters
Keyword ref is use for passing the value type by reference.
Passing parameter by reference:
class Program
{
//passing parameter by reference
public void increment(ref int i)
{
++i;
}
static void Main(string[] args)
{
Program p = new Program();
int j = 10;
p.increment(ref j);
Console.WriteLine(j);
Console.ReadKey();
}
}
11. Out parameters
Out parameters is used when we want multiple values output from single method and
don’t know the initialization value of input parameter.
class Program
{
//passing out parameter
public int initialize(out int i)
{
i = 10;
int j = i + 1;
return j;
}
static void Main(string[] args)
{
Program p = new Program();
int j;
int increasedvalue = p.initialize(out j);
Console.WriteLine("value and incremented value is {0} {1}", j, increasedvalue);
Console.ReadKey();
}
}
12. Parameter arrays
Passing array as parameter:class Program
{
//passing array as parameter
public int[] initialize(int[] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
arr[i] = i;
}
return arr;
}
static void Main(string[] args)
{
Program p = new Program();
int[] arr = new int[10];
// passing array as parameter;
p.initialize(arr);
for (int i = 0; i < arr.Length - 1; i++)
{
Console.WriteLine(arr[i]);
}
Console.ReadKey();
}
}
13. Optional and Named parameters
Using Optional parameters:
class Program
{
// length and breadth are optional parameter
public void print(int pages, int length = 100, int breadth = 40)
{
Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages);
}
static void Main(string[] args)
{
Program p = new Program();
p.print(10);
Console.ReadKey();
}
}
14. Optional and Named parameters
Using Named parameters:
class Program
{
public void print(int pages, int length, int breadth)
{
Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages);
}
static void Main(string[] args)
{
Program p = new Program();
// passing parameters by name
p.print(length: 10, breadth: 16, pages: 10);
Console.ReadKey();
}
}