Bhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.com
www.dotnetvideotutorial.com
For video visit
www.dotnetvideotutorial.com
Methods
Passing
value types
By Value
By
Reference
Passing
reference
types
By Value
By
Reference
Agenda
Features
Variable
arguments
Named
arguments
Optional
parameters
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
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()
Method without parameter and No return value
static void Main(string[] args)
{
greet();
Console.ReadKey();
}
static void greet()
{
Console.WriteLine("Hello");
}
www.dotnetvideotutorial.com
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
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
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
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
 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
static void Main(string[] args)
{
int no1 = 25, no2 = 50;
Console.WriteLine("Before swaping: no1 = {0}, no2 = {1}", no1, no2);
Swap(ref no1, ref no2);
Console.WriteLine("After swaping: no1 = {0}, no2 = {1}", no1, no2);
Console.ReadKey();
}
static void Swap(ref int no1,ref int no2)
{
int temp = no1;
no1 = no2;
no2 = temp;
}
ref keyword
25
no1
5420
50
no2
5424
5420
no1
5424
no2
25
temp
50 25
www.dotnetvideotutorial.com
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
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
Passing string by value
static void Main(string[] args)
{
string str = "Milkyway";
Console.WriteLine("str = " + str);
ChangeString(str);
Console.WriteLine("nstr = " + str);
}
static void ChangeString(string str)
{
Console.WriteLine("nstr = " + str);
str = "Andromeda";
Console.WriteLine("nstr = " + str);
}
5028 Milkyway
Andromeda
5028
5028
6252
6252
str
str
www.dotnetvideotutorial.com
Passing string by reference
static void Main(string[] args)
{
string str = "Milkyway";
Console.WriteLine("str = " + str);
ChangeString(ref str);
Console.WriteLine("nstr = " + str);
Console.ReadKey();
}
static void ChangeString(ref string str)
{
Console.WriteLine("nstr = " + str);
str = "Andromeda";
Console.WriteLine("nstr = " + str);
}
5028 Milkyway
Andromeda
5028
6252
6252
2020
2020
str
str
www.dotnetvideotutorial.com
Passing Array by value
static void Main(string[] args)
{
int[] marks = new int[] { 10, 20, 30 };
Console.WriteLine(marks[0]);
ChangeMarks(marks);
Console.WriteLine(marks[0]);
}
static void ChangeMarks(int[] marks)
{
Console.WriteLine(marks[0]);
marks[0] = 70;
Console.WriteLine(marks[0]);
}
5028
5028
10 20 30
5028
marks
marks
70
www.dotnetvideotutorial.com
Flexibility
Variable
Arguments
Named
Arguments
Optional
Parameters
Passing variable number of arguments
static void Main(string[] args)
{
Console.Write("Humans : ");
printNames("Neo", "Trinity", "Morphious", "Seroph");
Console.Write("Programs : ");
printNames("Smith", "Oracle");
}
static void printNames(params string[] names)
{
foreach (string n in names)
{
Console.Write(n + " ");
}
Console.WriteLine();
}
www.dotnetvideotutorial.com
Named Arguments
static void Main(string[] args)
{
int area1 = CalculateArea(width:10,height:15);
Console.WriteLine(area1);
int area2 = CalculateArea(height: 8, width: 4);
Console.WriteLine(area2);
}
static int CalculateArea(int width, int height)
{
return width * height;
}
www.dotnetvideotutorial.com
Optional Parameters
static void Main(string[] args)
{
Score(30, 70, 60, 80);
Score(30, 70, 60);
Score(30, 70);
}
static void Score(int m1,int m2,int m3 = 50,int m4 = 30)
{
Console.WriteLine(m1 + m2 + m3 + m4);
}
www.dotnetvideotutorial.com
Bhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.com
www.dotnetvideotutorial.com

Methods

  • 1.
  • 2.
  • 3.
    Methods Passing value types By Value By Reference Passing reference types ByValue By Reference Agenda Features Variable arguments Named arguments Optional parameters
  • 4.
    Methods  A methodis 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 • Methodwithout 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 parameterand 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 Parametersand 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 voidMain(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 Parameterbut 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 andout 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
  • 12.
    static void Main(string[]args) { int no1 = 25, no2 = 50; Console.WriteLine("Before swaping: no1 = {0}, no2 = {1}", no1, no2); Swap(ref no1, ref no2); Console.WriteLine("After swaping: no1 = {0}, no2 = {1}", no1, no2); Console.ReadKey(); } static void Swap(ref int no1,ref int no2) { int temp = no1; no1 = no2; no2 = temp; } ref keyword 25 no1 5420 50 no2 5424 5420 no1 5424 no2 25 temp 50 25 www.dotnetvideotutorial.com
  • 13.
    out keyword static voidMain(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
  • 15.
    Passing string byvalue static void Main(string[] args) { string str = "Milkyway"; Console.WriteLine("str = " + str); ChangeString(str); Console.WriteLine("nstr = " + str); } static void ChangeString(string str) { Console.WriteLine("nstr = " + str); str = "Andromeda"; Console.WriteLine("nstr = " + str); } 5028 Milkyway Andromeda 5028 5028 6252 6252 str str www.dotnetvideotutorial.com
  • 16.
    Passing string byreference static void Main(string[] args) { string str = "Milkyway"; Console.WriteLine("str = " + str); ChangeString(ref str); Console.WriteLine("nstr = " + str); Console.ReadKey(); } static void ChangeString(ref string str) { Console.WriteLine("nstr = " + str); str = "Andromeda"; Console.WriteLine("nstr = " + str); } 5028 Milkyway Andromeda 5028 6252 6252 2020 2020 str str www.dotnetvideotutorial.com
  • 17.
    Passing Array byvalue static void Main(string[] args) { int[] marks = new int[] { 10, 20, 30 }; Console.WriteLine(marks[0]); ChangeMarks(marks); Console.WriteLine(marks[0]); } static void ChangeMarks(int[] marks) { Console.WriteLine(marks[0]); marks[0] = 70; Console.WriteLine(marks[0]); } 5028 5028 10 20 30 5028 marks marks 70 www.dotnetvideotutorial.com
  • 18.
  • 19.
    Passing variable numberof arguments static void Main(string[] args) { Console.Write("Humans : "); printNames("Neo", "Trinity", "Morphious", "Seroph"); Console.Write("Programs : "); printNames("Smith", "Oracle"); } static void printNames(params string[] names) { foreach (string n in names) { Console.Write(n + " "); } Console.WriteLine(); } www.dotnetvideotutorial.com
  • 20.
    Named Arguments static voidMain(string[] args) { int area1 = CalculateArea(width:10,height:15); Console.WriteLine(area1); int area2 = CalculateArea(height: 8, width: 4); Console.WriteLine(area2); } static int CalculateArea(int width, int height) { return width * height; } www.dotnetvideotutorial.com
  • 21.
    Optional Parameters static voidMain(string[] args) { Score(30, 70, 60, 80); Score(30, 70, 60); Score(30, 70); } static void Score(int m1,int m2,int m3 = 50,int m4 = 30) { Console.WriteLine(m1 + m2 + m3 + m4); } www.dotnetvideotutorial.com
  • 22.