methods

22
Methods Bhushan Mulmule [email protected] www.dotnetvideotutorial.com

Upload: bhushan-mulmule

Post on 10-May-2015

637 views

Category:

Technology


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Methods

MethodsBhushan Mulmule

bhushan.mulmule@dotnetvideotutorial.comwww.dotnetvideotutorial.com

Page 2: Methods

For video visit www.dotnetvideotutorial.com

Page 3: Methods

Methods

Passing value types

By Value

By Reference

Passing reference types

By Value

By Reference

Agenda

Features

Variable arguments

Named arguments

Optional parameters

Page 4: Methods

MethodsA 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

Page 5: Methods

Possible Combinations• Method without parameter and

no return valuevoid 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()

Page 6: Methods

Method without parameter and No return value

static void Main(string[] args) { greet();

Console.ReadKey(); }

static void greet() { Console.WriteLine("Hello"); }

www.dotnetvideotutorial.com

Page 7: Methods

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

Page 8: Methods

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; }

no110

no220

no110

no220

result30

result30

www.dotnetvideotutorial.com

Page 9: Methods

Compact Format

static void Main(string[] args){ Console.WriteLine(add(25,50));}

static int add(int no1, int no2){ return no1 + no2;}

no125

no250

www.dotnetvideotutorial.com

Page 10: Methods

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

Page 11: Methods

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

Page 12: Methods

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

542050

no2

5424

5420

no1542

4

no2

25temp

50 25

www.dotnetvideotutorial.com

Page 13: Methods

out keywordstatic 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

radius5

area542

4

circum542

8

31.4

www.dotnetvideotutorial.com

Page 14: Methods

Passing Reference TypesReference 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 passed502

82000 5028

obj

www.dotnetvideotutorial.com

Page 15: Methods

Passing string by valuestatic 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

Page 16: Methods

Passing string by referencestatic 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

Page 17: Methods

Passing Array by valuestatic 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

Page 18: Methods

Flexibility

Variable Arguments

Named Argum

ents

Optional

Parameters

Page 19: Methods

Passing variable number of argumentsstatic 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

Page 20: Methods

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

Page 21: Methods

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

Page 22: Methods

Bhushan [email protected]

www.dotnetvideotutorial.com