Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generics: What is generic constraining interface?

Tags:

c#

generics

On MSDN - C# Programming guide Constraints on Type Parameters, it says:

where T : interface_name

The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic.

Could somebody kindly explain, what it means to have a generic interface? And explain how that can be a constraint and what it provides?

A simple example and a simple explanation is highly appreciated.

Many thanks in advance : )

like image 743
Idrees Avatar asked Dec 12 '22 12:12

Idrees


1 Answers

You can use a generic interface as a constraint. For example:

class MyClass<T> where T:IEnumerable<string>

you can even substitute the generic parameter of the type you define into your constraint:

class MyClass<T> where T:IEnumerable<T>
like image 101
CodesInChaos Avatar answered Dec 29 '22 06:12

CodesInChaos