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.
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:Generic method type parameters are distinct from interface/class type parameters — I wouldn’t expect them to be implemented using
IModelin your example. (In other words, the T inIRepository<T>is not the T inGet<T>.)