Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading with same parameter signature

Tags:

c#

overloading

In C#, is it possible to have same parameters yet override each other(they are different in the return types)

public override Stocks[] Search(string Field,string Param){ //some code}
public override Stocks Search(string Field, string Param){//some code}

C# returns compilation error

like image 295
Soham Avatar asked May 01 '10 16:05

Soham


People also ask

Can overloading have same parameters?

No, you cannot overload a method based on different return type but same argument type and number in java. same name. different parameters (different type or, different number or both).

Can overloaded methods have the same name?

Two or more methods can have the same name inside the same class if they accept different arguments. This feature is known as method overloading.

What is signature in overloading?

Signatures are the enabling mechanism for overloading of members in classes, structs, and interfaces. Overloading of methods permits a class, struct, or interface to declare multiple methods with the same name, provided their signatures are unique within that class, struct, or interface.

What happens when method signature is the same and the return type is different?

The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types. It will throw a compile-time error. If both methods have the same parameter types, but different return types, then it is not possible.


2 Answers

In C#, you can only overload methods that have different signatures.

The return type of a method is not included in the signature - only the method name, types and number of parameters (and their order). The two examples have the same signature, so they cannot exist together.

Classically, one can return a list of items (array or other data structure) - if only one item is required, you simply return a list with one item.

like image 119
Oded Avatar answered Oct 12 '22 23:10

Oded


As Oded already points out in his answer, it is not possible to overload a method when the only difference is the return type.

public override Stocks[] Search(string Field,string Param){ //some code}
public override Stocks Search(string Field, string Param){//some code}

Think about it: How should the compiler know which method variant to call? This apparently depends on your search result, and obviously the compiler can't know that in advance.

In fact, what you want is one function which has two possible return types. What you don't want is two separate methods, because you'd then have to decide up-front which one to call. This is obviously the wrong approach here.

One solution is to always return an array; in case where only one Stocks object is found, you return an array of size 1.

like image 34
stakx - no longer contributing Avatar answered Oct 12 '22 23:10

stakx - no longer contributing