13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Advanced generics87with<strong>in</strong> another, and types with multiple generic parameters. This sounds a lot morecomplicated, but it works as you probably th<strong>in</strong>k it should. List<strong>in</strong>g 3.8 shows this <strong>in</strong>action, this time us<strong>in</strong>g static constructors to show just how many types there are.List<strong>in</strong>g 3.8Static constructors with nested generic typesNote!Only 5 l<strong>in</strong>esof output…public class Outer{public class Inner{static Inner(){Console.WriteL<strong>in</strong>e("Outer.Inner",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, sothe output of list<strong>in</strong>g 3.8 looks like this:Outer.InnerOuter.InnerOuter.InnerOuter.InnerOuter.InnerJust as with nongeneric types, the static constructor for any closed type is only executedonce, which is why the last l<strong>in</strong>e of list<strong>in</strong>g 3.8 doesn’t create a sixth l<strong>in</strong>e of output—thestatic constructor for Outer.Inner executed earlier,produc<strong>in</strong>g the second l<strong>in</strong>e of output. To clear up any doubts, if we had a nongenericPla<strong>in</strong>Inner class <strong>in</strong>side Outer, there would still have been one possibleOuter.Pla<strong>in</strong>Inner type per closed Outer type, so Outer.Pla<strong>in</strong>Inner wouldbe separate from Outer.Pla<strong>in</strong>Inner, with a separate set of static fields asseen earlier.Now that we’ve seen just what constitutes a different type, we should th<strong>in</strong>k aboutwhat the effects of that might be <strong>in</strong> terms of the amount of native code generated.And no, it’s not as bad as you might th<strong>in</strong>k…Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!