Partial Types

Partial types allow the declaration of Classes and Records to be splot into multiple parts and, potentially, multiple files within the same project.

A class or recored can be declared as partial simply by applying the partial Type Modifider to it's declaration. Once done, multiple declarations for the same type may exist (as long as they are all marked as partial), and will be combined into a single type when compiled.

For Classes, only a single base class can of course be specified (Records, of course, have no ancestor). It is allowed either for all partial declarations to declare the same base class, or for only one part to declare it. If different parts declare a different ancestor, compilation will fail.

All parts also must declare the same Visibility and, of course, be within the same Namespace (otherwise, they'd be unique, separate types).

It is entirely permissable for the partial modifier to be present on a class that is declared with only one partial – for example in preparation for additional parts ot be added later, or because the other parts are not used in the current project or configuration.

type
  MyClass = public partial class(MyBaseClass)
  public
    method Test;
  end;
type
  MyClass = public partial class
  public
    method Test2;
  end;

Partial Methods

Partial types can also declare partial Methods, which can be used to advertise the availability of a method in one part, and optionally provide an implementation on the other part.

For this, one part must declare the partial method with both the partial and the empty Member Modifiers and not provide an implementation. Optionally, another part may re-declare the method with just the partial Member Modifier, and provide an implementation.

A method declared as such can be called from other pieces of code, like any other method. If only the empty part is provided, calls to the method become a no-op, and have no effect. If an implementation is provided in another part, calls to the method will, of course, call that implementation.

type
  MyClass = public partial class(MyBaseClass)
  public
    method Test;
    begin
      Test2;
    end;
    method TestHelper; partial; empty;
  end;
type
  MyClass = public partial class
  public
    method TestHelper; partial;
    begin
      DoSomething;
    end;
  end;

Partial methods are useful when one part of the class needs to refer to a method that might or might not be implemented in another part. For example, the implementation for a partial method might be in a part that is conditionally compiled, say for Debug vs. Release, or for a particular platform.

Or one part might be machine-generated (such as the code-behind file for a WinForms or WPF form, and have calls that the developer might or might not choose to provide in the user-edited part of the class.

Modifiers

A partial type is a Class or Record that is marked with the p[artial Type Modifier.

The Visibility modifier needs to match between all parts of the type, or has to be omitted from the other parts. In other words, of one part declared a class as public.

See Also