What
are the different ways of overloading in C# (C Sharp)?
There are
three different ways of overloading in C#. They are listed below:
Method Overloading: Same method name can be overloaded with different
set of arguments in its signature.
Constructor Overloading: Constructor of a class can be overloaded
with a different set of arguments in its signature.
Operator Overloading: Every operator of C# has predefined meaning.
C# provides you an option to give a new meaning for an operator using
operator overloading.
Here is an
example demonstrating method overloading in C#:
class sampleClass
{
int member1;
string member2;
public void setData(int data) {
member1 = data;
}
public void setData(string data) {
member2 = data;
}
public static void Main() {
sampleClass obj = new sampleClass();
obj.setData(10);
obj.setData(Hello);
}
}