Where do generic interfaces are quite useful ? ( I am a beginner,so simple example will definitely helpful).
Its useful when you need an interface, but you also need to abstract the data type. Simple example
public interface IMyShape<T>
{
T X { get; }
T Y { get; }
}
public class IntSquare : IMyShape<int>
{
int X { get { return 100; } }
int Y { get { return 100; } }
}
public class IntTriangle : IMyShape<int>
{
int X { get { return 200; } }
int Y { get { return 200; } }
}
public class FloatSquare : IMyShape<float>
{
float X { get { return 100.05; } }
float Y { get { return 100.05; } }
}
你可以先看一下IEnumerable<T>
。
Generic interfaces are really useful when you want to parameterize the types of one of the members in the interface. Consider the IEnumerable
and IEnumerable<T>
interfaces. The first iterates Objects
whereas the second iterates instances of the type argument supplied for T
.
Since interfaces can be generic it allows you to leverage their flexibility while still taking advantage of generics in the same way that you would for a concrete type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.