Hi
How can we overload a method in C#?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Jul 24, 2012, 1:33 AM
Hi,
C# allows us to define multiple functions with the same name differing in the number type and order of arguments. This is termed as function overloading.
static void ShowMyValue()
{
ShowMyValue("No Value Define.");
}
static void ShowMyValue(string value)
{
Console.WriteLine(value);
}
hope this will help you.
Satyapriya NayakPosted Jul 23, 2012, 11:27 AM
In Vb.net
Overloading: - It is a process of using several procedures with the same name but having different signatures. The signature changes in number of parameters, type of parameter, and order of parameter.
Program
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
raj()
raj(2)
raj(3, 3)
raj("ravi", 1)
raj(100, "rahul")
End Sub
Public Overloads Sub raj()
MsgBox("Hello Raj")
End Sub
Public Overloads Sub raj(ByVal x As Integer)
MsgBox(x * 5)
End Sub
Public Overloads Sub raj(ByVal x As Integer, ByVal y As Integer)
MsgBox(x + y)
End Sub
Public Overloads Sub raj(ByVal s As String, ByVal x As Integer)
MsgBox(s & ":has achieved rank" & x)
End Sub
Public Overloads Sub raj(ByVal x As Integer, ByVal s As String)
MsgBox(s & ":has secured" & x)
End Sub
End Class
Thanks
Nattudurai EswaramurthyPosted Jul 23, 2012, 8:19 AM
public int Add()
{
return 10;
}
public int Add(int a, int b)
{
return a+b;
}
public float Add(float a, float b)
{
return a+b;
}
Santhosh Kumar JayaramanPosted Jul 23, 2012, 6:27 AM
http://www.codeproject.com/Articles/17951/Function-Overloading
VulpesPosted Jul 23, 2012, 6:26 AM
public void MyMethod()
{
// code
}
public void MyMethod(int i)
{
// code
}
public string MyMethod(int i, string s)
{
// code
}
shows 3 overloads of MyMethod.
Notice that the return type of a method plays no part in overload resolution and so needn't be the same for every overload.