Reputation:
I am having two methods int Add(int, int)
and long Add(long, long)
.
When I call this method the call gates resolved on the basis of the size of the parameter I am passing to these method.
How does C# compiler identifies which method to call?
I guess it resolves with size of the parameter which we are passing to the method.
if parameter size fits in datatype(int) it will call int Add(int, int)
otherwise it will call long Add(long, long)
.
This is my guess, please conform the same and clarify how call to these methods got resolved?
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Add(2, 3);
p.Add(223232323777, 3);
}
long Add(long a, long b)
{
return a + b;
}
int Add(int a, int b)
{
return a + b;
}
}
Upvotes: 2
Views: 3111
Reputation: 8111
Your assumption is true. As long as both numbers fit into an int
the second method is called. The second parameter is implicitely converted to long
(http://msdn.microsoft.com/en-us/library/y5b434w4.aspx) to match the signature.
You can also force the use of the first method for a smaller number:
Add((long)2, 3);
or
Add(2L, 3);
Upvotes: 2