Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2010 Implement Generic Interface expansion doesn't use specified type

Using the release version of Visual Studio 2010 I think there's a difference in the "Implement Interface" expansion from VS2008

If I speicify an interface and implement it in a class as so:

public interface IRepository<T> where T : IModel
{
    T Get<T>(int id);
    void Update<T>(T item);
    int Add<T>(T item);
}    

public class MockRepository : IRepository<MockUser>
{
// ...
}

Then use the "Implement Interface" expansion and get this:

public class MockRepository : IRepository<MockUser>
{
    public T Get<T>(int id)
    {
        throw new NotImplementedException();
    }

    public void Update<T>(T item)
    {
        throw new NotImplementedException();
    }

    public int Add<T>(T item)
    {
        throw new NotImplementedException();
    }
}

Instead of what I expected

public class MockRepository : IRepository<MockUser>
{
    public MockUser Get<MockUser>(int id)
    {
        throw new NotImplementedException();
    }

    public void Update<MockUser>(MockUser item)
    {
        throw new NotImplementedException();
    }

    public int Add<MockUser>(MockUser item)
    {
        throw new NotImplementedException();
    }
}

The IDE uses the type variable name from the generic interface definition T instead of the specified concrete type MockUser. Is this a bug? Or is something new just for VS2010 / .Net 4.0?

Update: This is NOT a bug, I didn't specify the interface as I inteded, it should be defined as:

public interface IRepository<T> where T : IModel
{
    T Get(int id);
    void Update(T item);
    int Add(T item);
}    

in other words I didn't need to specify the Type parameter T at the interface and method level, but only at the interface.

like image 888
TJB Avatar asked Apr 19 '26 10:04

TJB


1 Answers

There's no purpose to <T> as a type parameter to the interface's methods. It's not necessary, and if you remove it, you'll get the expected behavior -- except that the result is this:

public class MockRepository : IRepository<IModel>
{
    public IModel Get(int id)
    {
        throw new NotImplementedException();
    }

    public void Update()
    {
        throw new NotImplementedException();
    }

    public int Add(IModel item)
    {
        throw new NotImplementedException();
    }
}

Generic method type parameters are distinct from interface/class type parameters -- I wouldn't expect them to be implemented using IModel in your example. (In other words, the T in IRepository<T> is not the T in Get<T>.)

like image 133
Ben M Avatar answered Apr 22 '26 01:04

Ben M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!