Member with the same signature already defined with different type constraints
Asked Answered
A

3

7

I have run across an issue with overloading methods that have different constraints that seems exclusive. That is my example:

public class A
{
    public void Do<T>() where T : class
    {

    }

    public void Do<T>() where T : struct 
    {

    }
}

And this does not compile with the following error "Member with the same signature already defined". Is it possible to satisfy both conditions at once or it's just the limitation of the C# compiler?

Attitudinarian answered 12/3, 2013 at 16:39 Comment(0)
W
10

It's not a limitation of the compiler - it's a limitation of the language (and quite possibly the CLR as well; I'm not sure).

Fundamentally those are clashing overloads - it's like trying to overload by return type. It's not supported.

It is possible to declare methods such that these calls all compile to invocations of different methods:

a.Do<int>();
a.Do<string>();
a.Do<int?>();

... but it always involves optional parameters and/or parameter arrays, and it's horrible.

Also note that although you can't overload by generic constraints, you can overload by the generic "arity" (the number of type parameters):

public void Foo() {}
public void Foo<T>() {}
public void Foo<T1, T2>() {}
Weakling answered 12/3, 2013 at 16:42 Comment(8)
At least I hope that it was correct to say that these two constraints are exclusive and it's just limitation, right? :)Attitudinarian
@IlyaChernomordik: It's a limitation, but a pretty reasonable one IMO.Weakling
But what's wrong with allowing this? I just need to do different actions depending on whether it's a class (I can check if it's null) or when it's just long (then I know that value is there). So the way out should be just giving different names to the methods it seems. I definitely don't want to use the horrible way :)Attitudinarian
FWIW: the language details that overloads must differ by signature ("unique signatures"). In C#, the constraints are not considered part of the signature. The signature is the name, the number of type parameter, the type, kind, and order of parameters. Return type is explicitly not included. But, also constraints are implicitly not included.Newt
@IlyaChernomordik No one said there's anything wrong with allowing this; just that the language doesn't currently allow it.Newt
@Peter Ritchie: I thought Jon Skeet means that it's wrong by saying that it's a pretty reasonable limitationAttitudinarian
@IlyaChernomordik: It's not so much "wrong" - as it's not worth the effort in designing, testing, documenting and implementing it (across multiple languages and the CLR).Weakling
@Jon Skeet: Perhaps it's not, but would be a nice to have feature. But thank you for explanations.Attitudinarian
B
2

You cannot overload a method by varing the generic parameter containts. For a valid method overload you have to have different input parameters to the method.

Brasier answered 12/3, 2013 at 16:42 Comment(0)
A
2

Both methods should have the following name when compiled:

A.Do``1

As the count of generic parameters goes into the name of the method or class.

Not sure what your situation is but you may need to make use of reflection to call those methods:

public class A
{
    public void Do<T>()
    {
        if(typeof(T).IsValueType){
            // nasty reflection to call DoValueType
        }
        else {
            // nasty reflection to call DoReferenceType
        }
    }
    private void DoReferenceType<T>() where T : class {

    }
    private void DoValueType<T>() where T : struct {

    }
}
Absorbefacient answered 12/3, 2013 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.