• Home
  •  Methods and Method Overloading

 Methods and Method Overloading

Agenda

C# Method

C# Program to illustrate how to create and access User defined method:

//Create a Method with returning Value and two parameters passed
public int add(int a, int b){
int result = a + b ;
return result;
}

//Create a Method without returning Value and without any parameter

public void helloWord()

{

Console.WriteLine("Welcome to Programming World");

}

//Create Object and access Method in the main method assumed Sample is the class name
Sample obj = new Sample();
int x = obj.add(10, 25);
Console.WriteLine(x);

Obj.helloWord();

Default Parameter Value Method

//declaring the method as Default Parameter Value

static void testCalendarDay(string calendarDay = "Monday")

{

Console.WriteLine(calendarDay);

}

static void Main()

{

testCalendarDay("Tuesday"); //Output Tuesday

testCalendarDay("Wednesday"); //Output Wednesday

testCalendarDay(); //Output Monday which is the Default Parameter Value

}

 public int add(int a, int b)
{ int result = a + b;
return result;
}

public int add(int a, int b, int c)
{
int result = a + b + c;
return result;
}

public double add(double a, double b)
{
double result = a + b;
return result;
}

public void helloWord()
{
Console.WriteLine("Welcome to Programming World");
}

public void helloWord(string language)
{
Console.WriteLine("Welcome to Programming World : " + language);
}

static void testCalendarDay(string calendarDay = "Monday")
{
Console.WriteLine(calendarDay);
}

Console.WriteLine("Welcome to C# World");
Console.WriteLine("Welcome to C# World");

Console.WriteLine("Welcome to C# World");
testCalendarDay("Tuesday");
testCalendarDay("Wednesday");
testCalendarDay();


Program obj = new Program();
int c = obj.add(1, 2);
Console.WriteLine(c);
Console.WriteLine(obj.add(10, 15, 20));
Console.WriteLine(obj.add(10.677, 92.7880)); obj.helloWord();
obj.helloWord("CSharp");