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.

Partial types185<strong>C#</strong> 2 allows more than one file to contribute to a type, and <strong>in</strong>deed IDEs can extendthis notion so that some of the code that is used for a type may not even be visible as<strong>C#</strong> 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 <strong>in</strong> partialtypes and allow a rich but efficient way of add<strong>in</strong>g manually written hooks <strong>in</strong>toautogenerated code. This is actually a <strong>C#</strong> 3 feature (this time based on feedback about<strong>C#</strong> 2), but it’s far more logical to discuss it when we exam<strong>in</strong>e partial types than to waituntil the next part of the book.7.1.1 Creat<strong>in</strong>g a type with multiple filesCreat<strong>in</strong>g a partial type is a c<strong>in</strong>ch—you just need to <strong>in</strong>clude the partial contextualkeyword <strong>in</strong> the declaration for the type <strong>in</strong> each file it occurs <strong>in</strong>. A partial type can bedeclared with<strong>in</strong> as many files as you like, although all the examples <strong>in</strong> this sectionuse two.The compiler effectively comb<strong>in</strong>es all the source files together before compil<strong>in</strong>g.This means that code <strong>in</strong> one file can call code <strong>in</strong> another and vice versa, as shown <strong>in</strong>figure 7.1—there’s no need for “forward references” or other tricks.You can’t write half of a member <strong>in</strong> one file and half of it <strong>in</strong> another—each <strong>in</strong>dividualmember has to be complete with<strong>in</strong> its own file. 1 There are a few obvious restrictionsabout the declarations of the type—the declarations have to be compatible. Anyfile can specify <strong>in</strong>terfaces to be implemented (and they don’t have to be implemented<strong>in</strong> that file); any file can specify the base type; any file can specify a type parameterconstra<strong>in</strong>t. However, if multiple files specify a base type, those base types have to bethe same, and if multiple files specify type parameter constra<strong>in</strong>ts, the constra<strong>in</strong>ts haveto be identical. List<strong>in</strong>g 7.1 gives an example of the flexibility afforded (while notdo<strong>in</strong>g anyth<strong>in</strong>g even remotely useful).Example1.cspartial class Example{void FirstMethod(){SecondMethod();}}void ThirdMethod(){}Example2.cspartial class Example{void SecondMethod(){ThirdMethod();}}Figure 7.1 Code <strong>in</strong> partial types is able to “see” all of the members of the type,regardless of which file each member is <strong>in</strong>.1There’s an exception here: partial types can conta<strong>in</strong> nested partial types spread across the same set of files.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!