In ???single file??? types, initialization of member and static variables is guaranteed to
occur in the order they appear in the file, but there??™s no guaranteed order when multiple
files are involved. Relying on the order of declaration within the file is brittle to
start with??”it leaves your code wide open to subtle bugs if a developer decides to
???harmlessly??? move things around. So, it??™s worth avoiding this situation where you can
anyway, but particularly avoid it with partial types.
Now that we know what we can and can??™t do, let??™s take a closer look at why we??™d
want to do it.
7.1.2 Uses of partial types
As I mentioned earlier, partial types are primarily useful in conjunction with designers
and other code generators. If a code generator has to modify a file that is ???owned??? by
a developer, there??™s always a risk of things going wrong. With the partial types model, a
Listing 7.1 Demonstration of mixing declarations of a partial type
Specifies interface
and type parameter
constraint
B
C
Implements
IEquatable
Specifies base
class and interface
D
E
Implements
IDisposable
187 Partial types
code generator can own the file where it will work, and completely overwrite the
whole file every time it wants to.
Some code generators may even choose not to generate a C# file at all until the
build is well under way.
Pages:
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368