Type constraints (classes and interfaces) C# with Example



Type constraints (classes and interfaces) C# with Example

Type constraints are able to force a type parameter to implement a certain interface or class. 
interface IType; 
interface IAnotherType; 
// T must be a subtype of IType 
interface IGeneric 
where T : IType 
{ 
} 
// T must be a subtype of IType 
class Generic 
where T : IType 
{ 
} 
class NonGeneric 
{ 
// T must be a subtype of IType 
public void DoSomething(T arg) 
where T : IType 
{ 
} 
} 
//  Valid  definitions  and  expressions: 
class Type : IType { } 
class Sub : IGeneric { } 
class Sub : Generic { } 
new NonGeneric().DoSomething(new Type()); 
// Invalid definitions and expressions: 
class AnotherType : IAnotherType { } 
 

class Sub : IGeneric { } 
class Sub : Generic { } 
new NonGeneric().DoSomething(new AnotherType()); 
Syntax for multiple constraints: 
class Generic 
where T : IType 
where T1 : Base, new() 
{ 
} 
Type constraints works in the same way as inheritance, in that it is possible to specify multiple interfaces as 
constraints on the generic type, but only one class: 
class A { /* ... */ } 
class B { /* ... */ } 
interface I1 { } 
interface I2 { } 
class Generic 
where T : A, I1, I2 
{ 
} 
class Generic2 
where T : A, B //Compilation error 
{ 
} 
Another rule is that the class must be added as the first constraint and then the interfaces: 
class Generic 
where T : A, I1 
{ 
} 
class Generic2 
where T : I1, A //Compilation error 
{ 
} 
All declared constraints must be satisfied simultaneously for a particular generic instantiation to work. There is no 
way to specify two or more alternative sets of constraints. 

0 Comment's

Comment Form

Submit Comment