Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open, Closed, Bound and Unbound Generic types

Tags:

generics

I've read a number of posts about this but I'm still not sure that I fully understand the definitions.

Here's what I think are examples of the different terms. Am I on the right track here, or do I still not understand the concepts. Thanks

Array<T TArray> - unbound and open.
Array<int> - bound and closed.
Array<Array<T TArray> - bound and open.
Array<Array<int>> - bound and closed.
like image 651
Al Carnali Avatar asked Dec 17 '25 15:12

Al Carnali


1 Answers

Unbound means something like typeof(Dictionary<,>). Unbound types are only interesting for Reflection and can only be used in typeof(), not in any other context. All unbounds types are closed types, the combination "unbound and open" is impossible.

Assuming T is a type parameter of the current class/method:

Dictionary<,> - unbound and closed
Dictionary<string, int> - constructed and closed
Dictionary<int, T> - constructed and open
Dictionary<string, List<T>> - constructed and open
NonGenericClass - bound and closed

Note that there is no such thing as List<Dictionary<,>> - unbound types cannot be used as type arguments, only directly in typeof(). A type is either unbound, or completely bound. And if a type is unbound, there's no place where it could refer to a type parameter, so the combination "unbound and open" is impossible.

like image 97
Daniel Avatar answered Dec 19 '25 06:12

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!