Constraining Generic Types with Examples
Learn how to restrict generic code to a specific set of types using the Concatenator class example.
We'll cover the following...
We'll cover the following...
Constraining the type of T
In most instances, we will want to limit the type of T in order to only allow a specific set of types to be used within our generic code. This is best explained through an example as follows:
Limiting the type of T
-
We have defined a class named
Concatenatorthat is using generic syntax and is also constraining the type ofTto be either an array of strings or an array of numbers via the ...