IEnumerable.GetEnumerator () and IEnumerable <T> .GetEnumerator ()
There is a common interface in the .net infrastructure IEnumerable<T>that inherits from the non-common IEnumerable, and both have a method GetEnumerator(). The only differences between the two GetEnumerator()are the return type. Now I have the same design, but when I compile the code, the compiler said:
MyInterface<T>.GetItem()'hides an inherited element MyInterface.GetItem()'. Use a new keyword if you needed to hide.
MyInterface<T>.GetItem()returns a specific type T, and MyInterface.GetItem()returns a type System.Object.
So, I think that if the BCL team commands compile the .net structure, they will get the same warning.
I think compiler warnings are not very good, what do you think? And how can I solve this problem? I mean, I want to get a specific type T when calling MyInterface<T>.GetItem()not only an instance of type System.Object.
Thanks in advance!: -)
Addition: I am talking about interfaces: IMyInterface inherits from IMyInterface, and both of them have the GetItem () method (IMyInterface.GetItem () returns the type T, and IMyInterface.GetItem () returns the type System. Object). The problem is that if our code has only these two interfaces , that is, non-derived concrete classes, we will encounter a compiler warning after compiling the source code.
, . :
public class SomeClassThatIsIEnumerable<T> : IEnumerable<T>
{
public IEnumerator<T> GetEnumerator()
{
// return enumerator.
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
}
, GetEnumerator , GetEnumerator , SomeClassThatIsIEnumerable IEnumerator, .
: :
public interface IMyInterface
{
object GetObject();
}
public interface IMyInterface<T> : IMyInterface
{
new T GetObject();
}
// implementation:
public class MyClass : IMyInterface<string>
{
public string GetObject()
{
return "howdy!";
}
object IMyInterface.GetObject()
{
return GetObject();
}
}
, IEnumerable<T> IEnumerable . , , .
, , . . , myTypeInstance.Method , MyType, ((MyBaseType) myTypeInstance).Method , .
public interface Interface1 {
object GetItem();
}
public interface Interface2<T> : Interface1 {
T GetItem();
}
public class MyBaseType : Interface1 {
public object GetItem() {
// implements Interface1.GetType()
return null;
}
}
public class MyType<T> : Interface1, Interface2<T> {
public new T GetItem() {
// Implements Interface2<T>.GetItem()
return default(T);
}
object Interface1.GetItem() {
// implements Interface1.GetItem()
return this.GetItem(); // calls GetItem on MyType
}
}
var myType = new MyType();
var myTypeResult = myType.GetItem(); // calls GetItem on MyType
var myBaseTypeResult = new ((MyBaseType) myType).GetItem(); // calls GetItem on MyBaseType