Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics open and closed constructed types

Tags:

c#

generics

Recently I noticed that generic constructed types can be open and closed. But I do not understand what they actually mean. Can you give a simple example?

like image 601
user196546 Avatar asked Nov 14 '09 17:11

user196546


People also ask

What is an open generic type?

An open generic type is simply a generic type whose type parameters have not been specified. For example, IEnumerable<> is an open generic type, and IEnumerable (or string or whatever) is a **closed generic type**, as its type parameter has been specified.

What are closed generics?

As noted in the Subsequent Procedures Policy Development Process Working Group (SubPro PDP WG) Final Report, a closed generic is "a TLD representing a string that is a generic name or term under which domains are registered and usable exclusively by the registry operator or its affiliates."

What is a constructed type?

A constructed type is an open type if and only if one or more of its type arguments is an open type. A constructed nested type is an open type if and only if one or more of its type arguments or the type arguments of its containing type(s) is an open type.

What is open generic C#?

The C# language defines an open type to be a type that's either a type argument or a generic type defined with unknown type arguments: All types can be classified as either open types or closed types. An open type is a type that involves type parameters. More specifically: A type parameter defines an open type.


2 Answers

In practice the terminology doesn't really matter much - I can't remember the last time I had to worry about it except when trying to write about it.

  • An unbound type has no type arguments specified
  • A constructed type has at least one type argument specified
  • A type parameter is an open type
  • An array type where the element type is open is an open type
  • An open constructed type has at least one type argument which is an open type
  • A closed type is any type which isn't open

(There are further rules for nested types. Consult the C# 3.0 spec section 4.4 for gory details.)

As an example of an open constructed type, consider:

public class NameDictionary<T> : Dictionary<string, T> 

The base class of typeof(NameDictionary<>) is:

  • Constructed because it specifies type arguments
  • Open because the second type argument (T) is an open type

The MSDN docs for Type.IsGenericType have quite a useful little table.

Just to reiterate, this is almost entirely unimportant in day to day use.

I'm generally in favour of knowing the correct terminology - particularly for things like "pass by reference" etc - but in this case it really, really doesn't come up very often. I would like to actively discourage you from worrying about it :)

like image 100
Jon Skeet Avatar answered Oct 04 '22 14:10

Jon Skeet


From MSDN:

A generic type or method is closed if instantiable types have been substituted for all its type parameters, including all the type parameters of all enclosing types. You can only create an instance of a generic type if it is closed.

So this works as List<int> is closed:

var list = Activator.CreateInstance(typeof(List<int>)); 

But this throws an exception at run-time because List<> is open:

var list = Activator.CreateInstance(typeof(List<>));                                                ↑ 
like image 26
dtb Avatar answered Oct 04 '22 13:10

dtb