Prev | Current Page 178 | Next

Jon Skeet

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


My aim with this section is to cover everything you??™re likely to want to know about
generics. It talks more about the CLR and framework side of things than the particular
syntax of the C# 2 language, although of course it??™s all relevant when developing
in C# 2. We??™ll start by considering static members of generic types, including type initialization.
From there, it??™s a natural step to wonder just how all this is implemented
5 The formula used for calculating the hash code based on the two ???part??? results comes from reading Effective
Java (Prentice Hall PTR, 2001) by Joshua Bloch. It certainly doesn??™t guarantee a good distribution of hash
codes, but in my opinion it??™s better than using a bitwise exclusive OR. See Effective Java for more details, and
indeed for many other useful tips.
6 http://www.ecma-international.org/publications/standards/Ecma-334.htm
86 CHAPTER 3 Parameterized typing with generics
under the covers??”although we won??™t be going so deep that you need a flashlight.
We??™ll have a look at what happens when you enumerate a generic collection using
foreach in C# 2, and round off the section by seeing how reflection in the .NET
Framework is affected by generics.
3.4.1 Static fields and static constructors
Just as instance fields belong to an instance, static fields belong to the type they??™re
declared in. That is, if you declare a static field x in class SomeClass, there??™s exactly
one SomeClass.


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