Methods, Passing value types by value and by reference, Passing reference types by value and by reference, Passing variable number of arguments, Named arguments, Optional parameters.
4. Methods
A method is a code block containing a series of statements.
Methods are declared within a class or struct by specifying
Access Modifier
Other Modifiers
Return value
Name of the method
Method parameters.
public static int add(int no1, int no2)
{
}
www.dotnetvideotutorial.com
5. Possible Combinations
• Method without parameter and no
return value
void fun1()
• Method with parameter and no return
value
void fun2(string name)
• Method with parameter and return
value
int fun3(int no1, int no2)
• Method without parameter but with
return value
string fun4()
6. Method without parameter and No return value
static void Main(string[] args)
{
greet();
Console.ReadKey();
}
static void greet()
{
Console.WriteLine("Hello");
}
www.dotnetvideotutorial.com
7. static void Main(string[] args)
{
greet("Mr. Anderson");
Console.ReadKey();
}
static void greet(string name)
{
Console.WriteLine("Hello " + name);
}
Method with Parameters but No Return value
Mr. Anderson
name
www.dotnetvideotutorial.com
8. Method With Parameters and Return value
static void Main(string[] args)
{
int no1 = 10, no2 = 20;
int result = add(no1, no2);
Console.WriteLine("Result = {0}", result);
Console.ReadKey();
}
static int add(int no1, int no2)
{
int result = no1 + no2;
return result;
}
no1
10
no2
20
no1
10
no2
20
result
30
result
30
www.dotnetvideotutorial.com
9. Compact Format
static void Main(string[] args)
{
Console.WriteLine(add(25,50));
}
static int add(int no1, int no2)
{
return no1 + no2;
}
no1
25
no2
50
www.dotnetvideotutorial.com
10. Fn without Parameter but with return value
static void Main(string[] args)
{
Console.WriteLine(GetTime());
Console.ReadKey();
}
static string GetTime()
{
string time = DateTime.Now.ToShortTimeString();
return time;
}
www.dotnetvideotutorial.com
11. ref and out keyword used to pass arguments by reference
ref: to pass references of initialized variables
out: to pass references of uninitialized variables
value type as well as reference type can be passed by
reference
Passing References
www.dotnetvideotutorial.com
13. out keyword
static void Main(string[] args)
{
float radius, area, circumference;
Console.WriteLine("Enter Radius of Circle: ");
radius = Convert.ToInt32(Console.ReadLine());
Calculate(radius, out area, out circumference);
Console.WriteLine("Area = " + area);
Console.WriteLine("Circumference = " + circumference);
}
static void Calculate(float radius, out float area, out float circumference)
{
const float pi = 3.14f;
area = 2 * pi * radius;
circumference = pi * radius * radius;
}
radius
5420
5
area
5424
circum
5428
78.5
radius
5
area
5424
circum
5428
31.4
www.dotnetvideotutorial.com
14. Passing Reference Types
Reference type can be passed by value as well by reference
If passed by value - content of object reference is passed
If passed by reference - reference of object reference is
passed
5028
2000 5028
obj
www.dotnetvideotutorial.com