Prev | Current Page 353 | Next

Jon Skeet

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

This was
clearly a brittle situation.
In other cases, code generators create source that is compiled alongside manually
written code. In C# 1, adding extra functionality involved deriving new classes from the
autogenerated ones, which is ugly. There are plenty of other scenarios where having an
unnecessary link in the inheritance chain can cause problems or reduce encapsulation.
For instance, if two different parts of your code want to call each other, you need virtual
methods for the parent type to call the child, and protected methods for the reverse situation,
where normally you??™d just use two private nonvirtual methods.
185 Partial types
C# 2 allows more than one file to contribute to a type, and indeed IDEs can extend
this notion so that some of the code that is used for a type may not even be visible as
C# source code at all. Types built from multiple source files are called partial types.
In this section we??™ll also learn about partial methods, which are only relevant in partial
types and allow a rich but efficient way of adding manually written hooks into
autogenerated code. This is actually a C# 3 feature (this time based on feedback about
C# 2), but it??™s far more logical to discuss it when we examine partial types than to wait
until the next part of the book.
7.1.1 Creating a type with multiple files
Creating a partial type is a cinch??”you just need to include the partial contextual
keyword in the declaration for the type in each file it occurs in.


Pages:
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365