Will read up on 'generics'
(MSDN:
http://msdn2.microsoft.com/en-us/library/512aeb7t.aspx)
Generics are the "big feature" in 2.0, in the same way (but much, much
more so) as LINQ is the "big feature" in 3.5. It is a big (and very
important) area, so I can't attempt to do it much justice, but in
short, the code says:
* ShowForm is a generic method that accepts a single "type parameter"
named (by convention) "T" [this is the <Tbit]
* The caller is only allowed to invoke ShowForm with types that derive
from Form (inclusive) [T : Form()], and which have a public
parameterless constructor [T : new()]
When the method is called, "T" is a template (a Type); broadly
speaking, if you call it with Form1 as the type parameter (by saying
ShowForm<Form1>()), then imagine doing a find-and-replace inside
ShowForm on "T" replaced with "Form1":
.... if(child is Form1) ...
.... Form1 newForm = new Form1();
etc
The big restriction is that you can only use members that *must* exist
for *any* template - i.e. the methods on System.Object, and anything
that are constraints [T : Form, new()] introduce. For instance, we can
only use newForm.MdiParent because the compiler knows that it is (at
minimum) a Form, and Form has an MdiParent.
(this is different to C++ templates, where-by it checks at compile
time for suitable members; .NET generics are actually runtime-based
(not compile-time), so must work for types the compiler doesn't yet
know about).
This also means you can't call the method with unsuitable type
arguments, i.e. ShowForm<StringBuilderwill fail at compile-time
because StringBuilder isn't a Form.
There is quite a lot more depth to generics (compile-time type-
parameter-inference, for example) - but a fascinating area, and one
that you are going to have to face at some point to work with .NET 2
(and above).
Perhaps the first generics example people see is List<T- i.e.
List<intdata = new List<int>();
data.Add(16);
....
data.Add(13);
data.Sort();
[etc; same things, but this time a whole generic type, not just a
generic method]
Marc