Hi
How can we overload a method in C#?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 23, 2012, 2:26 AM EDT
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.
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jul 23, 2012, 2:27 AM EDT
http://www.codeproject.com/Articles/17951/Function-Overloading
Nattudurai EswaramurthyRank #5,536 · 296 reputationPosted Jul 23, 2012, 4:19 AM EDT
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;
}
Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 23, 2012, 7:27 AM EDT
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
Jignesh TrivediRank #16 · 50.3K reputationPosted Jul 23, 2012, 9:33 PM EDT
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.