Prev | Current Page 354 | Next

Jon Skeet

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

A partial type can be
declared within as many files as you like, although all the examples in this section
use two.
The compiler effectively combines all the source files together before compiling.
This means that code in one file can call code in another and vice versa, as shown in
figure 7.1??”there??™s no need for ???forward references??? or other tricks.
You can??™t write half of a member in one file and half of it in another??”each individual
member has to be complete within its own file.1 There are a few obvious restrictions
about the declarations of the type??”the declarations have to be compatible. Any
file can specify interfaces to be implemented (and they don??™t have to be implemented
in that file); any file can specify the base type; any file can specify a type parameter
constraint. However, if multiple files specify a base type, those base types have to be
the same, and if multiple files specify type parameter constraints, the constraints have
to be identical. Listing 7.1 gives an example of the flexibility afforded (while not
doing anything even remotely useful).
1 There??™s an exception here: partial types can contain nested partial types spread across the same set of files.
partial class Example
{
void FirstMethod()
{
SecondMethod();
}
void ThirdMethod()
{
}
}
partial class Example
{
void SecondMethod()
{
ThirdMethod();
}
}
Example1.


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