Partial methods

 

Introduction

A partial class or structure can contain a partial method. A part of the class contains the method's signature.  An optional implementation can be defined in the same part or in another part.

 

 

Defining a partial method

To define a method as being a partial method, simply select it the corresponding operation in the explorer and then add the "CsPartial" tagged value without parameters using the icon in the auxiliary window (see below).

 

Adding the "CsPartial" tagged value to create an extension method

 

Key:

1.      The "CsPartial" tagged value.

 

 

Objecteering C# Developer will then position the new "partial" modifier juste before the void keyword during code generation.

 

The generator will ignore the "CsPartial" tagged value if the method has a return parameter, one or more "out" parameters, or a virtual, abstract, override, new or sealed modifier. Furthermore, the visibility of the method must be private or default (implicitly private).

 

UML model

C# generated

 

 

 

// Definition in file1.cs

using System;

using System.Diagnostics;

 

namespace Project.Definition

{

    public partial class PartialClass

    {

        partial void PartialMethod();

    }

}

 

 

// Implementation in file2.cs

using System;

using System.Diagnostics;

 

namespace Project.Implementation

{

    public partial class PartialClass

    {

        public void PartialMethod()

        {

            // implementation code

        }

    }

}

 

Example of using partial types and methods

 

Note 1:   A partial method containing a "CsCode" or "CsReturned" note will generate the implementation part. The definition part will be produced if the partial method does not have these notes.

 

Note 2: A partial method declaration is made up of two parts: the definition and the implementation.  It is up to the user to only create a single definition/implementation couple.  Be careful, Objecteering C# Developer runs no tests on this question.