Google and read on what is
Static & Dynamic Polymorphism.
OR
Google and read on what is
Compile time & Runtime Polymorphism.
Further, as such it's not ASP.NET related, its OOP's related and thus
you follow it while writing/designing code in a given language.
When inheriting from another class, you may wish to change the
default bahaviour of a method or create a different method signature.
You can do this by overloading the method with your own code.
Method
signatures are formed from the parameter list, in particular the data
types. Methods can share the same name within the class as long as the
signature is different.
static int Add(int a, int b)
{
return a + b;
}
In the above example the signature
should be (int, int). In the Visual Studio code editor, if you were to
call the Add method, after you enter the '(' Intellisense will popup the
available parameters. Notice that there is only one option given to
you.
We can add an overloaded method with a different signature
and IntilliSense will now present two options, one for each signature of
the overloaded method. The compiler finds two methods of the same name
and two calls to that method with different parameters. It is able to
tell the difference by comparing the signatures of the methods.
static int Add(int a, int b, int c)
{
return a + b + c;
}
The names of the parameter and the return type have no effect on the method signature.
static decimal Add(int a, int b, int c)
{
return a + b + c;
}
The above method will raise an error because a method already exists for Add(int, int), even though it returns a decimal.
Methods
should be overloaded when you have similar methods that require
differing parameters or you want to add new functionality to existing
code, however you should not use overloading to often as it causes
headaches during debugging and testing and is more effort to maintain.
Optional Parameters in C#
C#
does not have an implementation of optional parameters like those found
in php, however you can simulate this feature using method overloading.
static int Add(int a, int b)
{
return Add(a, b, 0, 0);
}
static int Add(int a, int b, int c)
{
return Add(a, b, c, 0);
}
static int Add(int a, int b, int c, int d)
{
return (a + b + c + d);
}
In this example we can call Add with
2, 3 or 4 parameters and only one functional method is called. The
others just pass the data around.
Override
Methods can be
overridden, replacing the default behaviour or extending it. In this
example we will override the ToString method for the ArrayList class.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
ArrayList myList = new ArrayList();
myList.Add("Hello");
myList.Add("World");
myList.Add("123456");
Console.WriteLine(myList.ToString());
}
}
You may expect the ToString to return
"Hello World 123456″, but it actually returns
"System.Collections.ArrayList", which isn't particularly helpful.
We can override this default behaviour by creating a new class, which inherits from the ArrayList.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
myArrayList myList = new myArrayList();
myList.Add("Hello");
myList.Add("World");
myList.Add("123456");
Console.WriteLine(myList.ToString());
}
}
class myArrayList : System.Collections.ArrayList
{
public override string ToString()
{
string result = "";
string[] theItems = (string[])base.ToArray(typeof(string));
foreach (string item in theItems)
{
result += " " + item;
}
return result;
}
}
The base keyword is used to refer to
the object. By default the override method will return base.ToString().
You can use this base object to call the non-overwritten method.
Now when we create an instance of our new class and call the ToString method we are given "Hello World 123456″.