Browse DevX
Sign up for e-mail newsletters from DevX


Implications and Repercussions of Partial Classes in the .NET Framework 2.0 : Page 2

Specifically designed to overcome the brittleness of tool-generated code, partial classes are a source-level, assembly-limited, non-object-oriented way to extend the behavior of a class. A number of advantages derive from intensive use of partial classes; for example, you can have multiple teams at work on the same component at the same time. In addition, you have a neat and elegant incremental way to add functionality to a class.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

What's a Partial Class, Anyway?
With the .NET Framework 2.0, it is possible to split the definition of a class or an interface over two or more source files. Each source file appears to contain an ordinary class definition from beginning to end. Multiple files in the same project declare the same class but add a distinct fragment of the needed code to the overall definition. In other words, each constituent source file contains a section of the class definition. All parts are then combined together when the application is compiled. Here's a quick example.

public class Person { public string FirstName; public string LastName; public int Age; public DateTime Birth; }

Today, you need to save the preceding code in the same file and add it to a project. By using partial classes, you can split the above declaration in two or more pieces, each going to a different file. Here's how:

public partial class Person { public string FirstName; public string LastName; }

You save this block of code to a file named, say, Person.Name.cs. Next, you create a second file and name it, say, Person.Birth.cs.

public partial class Person { public int Age; public DateTime Birth; }

Figure 1. Class Declaration from Multiple Files: The compiler composes a class declaration from two or more distinct source files.
With any compiler other than the .NET 2.0 compilers, you get an error. An ambiguous reference to the class Person is at the origin of the error. It would be hard for the compiler to figure out which one is the right Person class to work with. The partial keyword does the trick. It informs the compiler that multiple blocks of code pertaining to the class can be found in various files on the command line. The compiler takes note of that and arranges a tree with the class representation as it finds partial sections of the class in the files on the command line (see Figure 1).

When the compiler processes a class declaration that is characterized by the partial keyword, it adds any member information to an internal table and generates the bytecode for it later in the compile process. As a result, all the members found in the various partial declarations are combined into an all-encompassing class definition. For partial classes to work, it is important that all classes involved sport the same class name.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date