1.

Is The Following Code Legal?

Answer»

using System;
namespace Demo
{
class Program
{
public static void Main()
{
}
public void Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
public int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
}
}

No, The above code does not compile. You cannot overload a METHOD based on the return type. To overload a method in C# either the number or type of parameters should be different. In general the return type of a method is not part of the SIGNATURE of the method for the purposes of method overloading. However, it is part of the signature of the method when DETERMINING the compatibility between a DELEGATE and the method that it points to.

using System;
namespace Demo
{
class Program
{
public static void Main()
{
}
public void Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
public int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
}
}

No, The above code does not compile. You cannot overload a method based on the return type. To overload a method in C# either the number or type of parameters should be different. In general the return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.



Discussion

No Comment Found