Variant interfaces C# with Example



Variant interfaces C# with Example

Interfaces may have variant type parameters. 
interface IEnumerable 
{ 
// ... 
} 
interface IComparer 
{ 
// ... 
 

} 
but classes and structures may not 
class BadClass // not allowed 
{ 
} 
struct BadStruct // not allowed 
{ 
} 
nor do generic method declarations 
class MyClass 
{ 
public T Bad(T1 t1) // not allowed 
{ 
// ... 
} 
} 
The example below shows multiple variance declarations on the same interface 
interface IFoo 
// T1 : Contravariant type 
// T2 : Covariant type 
// T3 : Invariant type 
{ 
// ... 
} 
IFoo foo1 = /* ... */; 
IFoo foo2 = foo1; 
// IFoo is a subtype of IFoo 

0 Comment's

Comment Form

Submit Comment