tags:

views:

334

answers:

3

Recently i noticed that Generics constructed types can be open and closed.But I am unable what do they actually mean.can you give a simple example?

+1  A: 

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<>));
                                               ↑
dtb
In that case it's an unbound type as well though - an open, constructed type is the more esoteric kind :)
Jon Skeet
+8  A: 

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 :)

Jon Skeet
Now a days most interview questions i faced are asked from your book.So for clarification purpose i asked. :) .
@generixs: If someone asked me that in an interview, I would confess that I wouldn't be able to say without consulting a book or the spec :) Not a very useful interview question, I have to say.
Jon Skeet
:) give me a portion of your brain ,so that i can answer as if i were a Jon Skeet ..(sorry for kidding..).
So in summary : if we find any type arguments then its a bound/constructed type (otherwise its unbound) and if we find any type parameters then its an open type (otherwise its closed). A closed type cannot be unbound. Did I leave any big ones out ?
mumtaz
@mumtaz: I'm afraid I'm too sleepy to say for sure. Basically I've *never* fully got my head round this terminology.
Jon Skeet
@Jon ... lolz... that was actually a great response. The more I read about them the more I feel like they were never intended for human consumption in the first place.
mumtaz
+1  A: 

I have mostly used open generics (basically uninstantiated generics) in dependency injection mappings. For example, something like

Bind<IRepository<>>()
   .To<BasicRepository<>>()

Then, when my object constructor contains:

public SomethingController(IRepository<Something>) { ... }

My dependency injection mechanism will instantiate a BasicRepository< Something > automagically. (This works with Ninject and StructureMap, and probably the Castle Windsor library; I'm not sure about other frameworks).

JasonTrue