Overloading :
This allows to define multiple methods in a class with the same name by changing their signatures.
Examples :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class overloading
{
// no input and out put parameters can be passed.
public void show()
{
Console.WriteLine("method1");
}
// one input parameter as int can be passed.
public void show(int x)
{
Console.WriteLine("method2");
}
// two input parameter as int's can be passed.
public void show(int x, int y)
{
Console.WriteLine("Method3");
}
// two input parameters as string ,int and char can passed.
public void show(string x, int y, char c)
{
Console.WriteLine("method4");
}
// Four input parameters as int, float, string , char and bool can be passed.
public void show(int x, float y, string z, char c, bool k)
{
Console.WriteLine("method5");
}
// one input parameter as char can be passed.
public void show(char c)
{
Console.WriteLine("method6");
}
// one input parameter as int can be passed.
public void show(double d)
{
Console.WriteLine("method7");
}
// one input parameter as decimal can passed
public void show(decimal dc)
{
Console.WriteLine("method8");
}
// one input parameter as long can be passed.
public void show(long l)
{
Console.WriteLine("method9");
}
// one input parameter as short can be passed.
public void show(short s)
{
Console.WriteLine("method10");
}
// two input parameters as string and int can be passed and return type must be string.
public string show(string s, int x)
{
Console.WriteLine("hell " + s + x);
return "hello";
}
// one input parameters as int and string can be passed and return type must be int.
public int show(int z, string s)
{
Console.WriteLine("this is integer method " +z + s);
return 1 ;
}
//public void show(byte b, sbyte sb, ushort us, uint ui)
//{
// Console.WriteLine("method11");
//}
// one input parameter as ulong
public void show(ulong ul)
{
Console.WriteLine("method12");
}
// one input parameter as int can be passed , different method and return type must be string.
public string show1 (int x)
{
Console.WriteLine("this is method " + x);
return "hello";
}
// No input and out put parameters and different method.
public void add()
{
Console.WriteLine("add method");
}
static void show(string s)
{
Console.WriteLine(s);
}
static string show(bool s)
{
Console.WriteLine(s);
return "show";
}
static void Main()
{
overloading o = new overloading();
o.show(20,"hello");
Console.WriteLine();
o.show();
Console.WriteLine();
o.show(1);
Console.WriteLine();
o.show(10,15);
Console.WriteLine();
o.show("hello",10,'c');
Console.WriteLine();
o.show(20,1.34f,"guraviah",'c', true);
Console.WriteLine();
o.show('c');
Console.WriteLine();
o.show(1.23);
Console.WriteLine();
o.show(50000m);
Console.WriteLine();
o.show(996626312111111111);
Console.WriteLine();
o.show(9966263121111111111);
Console.WriteLine();
o.add();
Console.WriteLine();
o.show1(12);
Console.WriteLine();
o.show("show ",10);
Console.WriteLine();
show("Kedar");
Console.WriteLine();
show(true) ;
Console.ReadLine();
}
}
}
Output :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class overconstructor
{
int x;
string y;
public overconstructor()
{
x = 10;
y ="lokesh";
}
public overconstructor(int x, string y)
{
this.x = x;
this.y = y;
}
//public overconstructor(string y)
//{
// this.y = y;
//}
public void display()
{
Console.WriteLine(x);
Console.WriteLine(y);
}
static void Main()
{
overconstructor o = new overconstructor();
overconstructor o1 = new overconstructor();
o.display();
o1.display();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class LoadParent
{
public void Test()
{
Console.WriteLine("method1");
}
public void Test(int x)
{
Console.WriteLine("method2 " + x);
}
public void Test(string s)
{
Console.WriteLine("method3");
}
}
}
Examples :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class overloading
{
// no input and out put parameters can be passed.
public void show()
{
Console.WriteLine("method1");
}
// one input parameter as int can be passed.
public void show(int x)
{
Console.WriteLine("method2");
}
// two input parameter as int's can be passed.
public void show(int x, int y)
{
Console.WriteLine("Method3");
}
// two input parameters as string ,int and char can passed.
public void show(string x, int y, char c)
{
Console.WriteLine("method4");
}
// Four input parameters as int, float, string , char and bool can be passed.
public void show(int x, float y, string z, char c, bool k)
{
Console.WriteLine("method5");
}
// one input parameter as char can be passed.
public void show(char c)
{
Console.WriteLine("method6");
}
// one input parameter as int can be passed.
public void show(double d)
{
Console.WriteLine("method7");
}
// one input parameter as decimal can passed
public void show(decimal dc)
{
Console.WriteLine("method8");
}
// one input parameter as long can be passed.
public void show(long l)
{
Console.WriteLine("method9");
}
// one input parameter as short can be passed.
public void show(short s)
{
Console.WriteLine("method10");
}
// two input parameters as string and int can be passed and return type must be string.
public string show(string s, int x)
{
Console.WriteLine("hell " + s + x);
return "hello";
}
// one input parameters as int and string can be passed and return type must be int.
public int show(int z, string s)
{
Console.WriteLine("this is integer method " +z + s);
return 1 ;
}
//public void show(byte b, sbyte sb, ushort us, uint ui)
//{
// Console.WriteLine("method11");
//}
// one input parameter as ulong
public void show(ulong ul)
{
Console.WriteLine("method12");
}
// one input parameter as int can be passed , different method and return type must be string.
public string show1 (int x)
{
Console.WriteLine("this is method " + x);
return "hello";
}
// No input and out put parameters and different method.
public void add()
{
Console.WriteLine("add method");
}
static void show(string s)
{
Console.WriteLine(s);
}
static string show(bool s)
{
Console.WriteLine(s);
return "show";
}
static void Main()
{
overloading o = new overloading();
o.show(20,"hello");
Console.WriteLine();
o.show();
Console.WriteLine();
o.show(1);
Console.WriteLine();
o.show(10,15);
Console.WriteLine();
o.show("hello",10,'c');
Console.WriteLine();
o.show(20,1.34f,"guraviah",'c', true);
Console.WriteLine();
o.show('c');
Console.WriteLine();
o.show(1.23);
Console.WriteLine();
o.show(50000m);
Console.WriteLine();
o.show(996626312111111111);
Console.WriteLine();
o.show(9966263121111111111);
Console.WriteLine();
o.add();
Console.WriteLine();
o.show1(12);
Console.WriteLine();
o.show("show ",10);
Console.WriteLine();
show("Kedar");
Console.WriteLine();
show(true) ;
Console.ReadLine();
}
}
}
Output :
example2 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class overload1
{
static void Main()
{
showstring(string.Empty);
showstring(".Net Developer");
Console.ReadLine();
}
static void showstring(string value)
{
if (value == string.Empty)
{
Console.WriteLine("Lokesh");
}
else
Console.WriteLine(value);
}
}
}
Output :
Example3 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class overconstructor
{
int x;
string y;
public overconstructor()
{
x = 10;
y ="lokesh";
}
public overconstructor(int x, string y)
{
this.x = x;
this.y = y;
}
//public overconstructor(string y)
//{
// this.y = y;
//}
public void display()
{
Console.WriteLine(x);
Console.WriteLine(y);
}
static void Main()
{
overconstructor o = new overconstructor();
overconstructor o1 = new overconstructor();
o.display();
o1.display();
Console.ReadLine();
}
}
}
Output :
Below two are Inheritance based Overloading:
Example4 : LoadParent.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class LoadParent
{
public void Test()
{
Console.WriteLine("method1");
}
public void Test(int x)
{
Console.WriteLine("method2 " + x);
}
public void Test(string s)
{
Console.WriteLine("method3");
}
}
}
Example 5 : LoadChild.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class LoadChild :LoadParent
{
public void Test(string s, int x)
{
Console.WriteLine("method4");
}
public void Test(char c)
{
Console.WriteLine("method5");
}
//public static void Test()
//{
// Console.WriteLine("method6");
//}
public void Test(long l)
{
Console.WriteLine("method6");
}
static void Main()
{
LoadChild lc = new LoadChild();
// Test();
lc.Test();
lc.Test(10);
lc.Test("lokesh");
lc.Test("guravaiah", 10);
lc.Test('C');
lc.Test(923131131131133);
Console.ReadLine();
}
}
}
Output :
No comments:
Post a Comment