Prev | Current Page 180 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

The concepts for different application domains work the same with generics as with nongeneric
types.
Listing 3.7 Proof that different closed types have different static fields
87 Advanced generics
within another, and types with multiple generic parameters. This sounds a lot more
complicated, but it works as you probably think it should. Listing 3.8 shows this in
action, this time using static constructors to show just how many types there are.
public class Outer
{
public class Inner
{
static Inner()
{
Console.WriteLine("Outer<{0}>.Inner<{1},{2}>",
typeof(T).Name,
typeof(U).Name,
typeof(V).Name);
}
public static void DummyMethod()
{
}
}
}
...
Outer.Inner.DummyMethod();
Outer.Inner.DummyMethod();
Outer.Inner.DummyMethod();
Outer.Inner.DummyMethod();
Outer.Inner.DummyMethod();
Outer.Inner.DummyMethod();
Each different list of type arguments counts as a different closed type, so
the output of listing 3.8 looks like this:
Outer.Inner
Outer.Inner
Outer.Inner
Outer.Inner
Outer.Inner
Just as with nongeneric types, the static constructor for any closed type is only executed
once, which is why the last line of listing 3.


Pages:
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192