Method Overloading Example
public class AddingNumbers
{
///
/// Method Overloading
///
/// Example : Console.WriteLine() & System.Math.Sign()
/// overloading a method when you for some reason need a couple of methods that take different perameters,
/// but conceptually do the same thing.
///
///
///
///
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
// Error'OOPS_Overloading.Program.AddingNumbers' already defines a member called 'Add' with the same parameter types C:\Documents and Settings\milindm\My Documents\Visual Studio 2005\Projects\OOPS_Overloading\OOPS_Overloading\Program.cs 21 27 OOPS_Overloading
/*
public string Add(int a, int b, int c)
{
return Convert.ToString(a + b + c);
}*/
}
static void Main(string[] args)
{
AddingNumbers obj = new AddingNumbers();
Console.WriteLine( "Result1: {0}",obj.Add(1,2));
Console.WriteLine("Result2: {0}", obj.Add(1, 2, 5));
Console.ReadKey();
}
Comments
Post a Comment