Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member with the same signature already defined with different type constraints

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?

like image 840
Ilya Chernomordik Avatar asked Mar 12 '13 16:03

Ilya Chernomordik


People also ask

What is the keyword which is used to apply constraints on type parameter?

Object, you'll apply constraints to the type parameter. For example, the base class constraint tells the compiler that only objects of this type or derived from this type will be used as type arguments. Once the compiler has this guarantee, it can allow methods of that type to be called in the generic class.

What is the purpose of the interface constraints on a type parameter?

Interface Type Constraint You can constrain the generic type by interface, thereby allowing only classes that implement that interface or classes that inherit from classes that implement the interface as the type parameter.

What are constraints in c#?

Constraints are like rules or instructions to define how to interact with a generic class or method. They can restrict the parameter that will be replaced with T to some certain type or class or have some properties, like to be new instance of class.


3 Answers

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>() {}
like image 173
Jon Skeet Avatar answered Nov 09 '22 07:11

Jon Skeet


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.

like image 32
Kevin Holditch Avatar answered Nov 09 '22 08:11

Kevin Holditch


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 {

    }
}
like image 37
Dandy Avatar answered Nov 09 '22 07:11

Dandy