Partial methods are a C# 3.0 technique used by code generators such as the one found in the LINQ to SQL Designer. They help to solve the age-old problem of allowing users to modify auto-generated code without fearing that their changes will be overwritten if the code is regenerated.

NOTE: The LINQ to SQL Designer is covered here.

When combined with partial classes, partial methods allow the developers of auto-generated code to reserve a name for a method that can be optionally implemented by the consumer of the class. This gives a developer a place where she can optionally add code that will never be overwritten if the code generation tool is re-run.

Listing 1: A simple example of a partial method

using System;

namespace PartialMethods
{
    // Assume this class is autogenerated
    public partial class MyPartialClass
    {
        partial void MyPartialMethod();

        public void CallPartialMethod()
        {
            MyPartialMethod();
        }
    }

    // Assume this class is written by hand
    public partial class MyPartialClass
    {
        partial void MyPartialMethod()
        {
            Console.WriteLine("Second half");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyPartialClass p = new MyPartialClass();
            p.CallPartialMethod();
        }
    }
}

Consider the code shown in Listing 1. Assume that the first instance of MyPartialClass was auto-generated by a visual designer that is part of Visual Studio. Notice that the class is declared as partial, as is MyPartialMethod. Notice that MyPartialMethod contains a header, but no implementation. You can consider this an invitation to the developer to implement this method if desired.

Assume that the developer took up the invitation and created by hand the second half of MyPartialClass. In this class she has written her implementation for MyPartialMethod, which will duly be called at runtime by the code in the first part of this partial class. If she had decided to decline the invitation, then the code to call MyPartialMethod would have been optimized out by the compiler, causing all traces of MyPartialMethod to be entirely eliminated at run time.

This system allows the developer to re-run the designer that created the first part of MyPartialClass without fear that this action would overwrite the code in her half of MyPartialClass. It also allows the developer to work with a relatively small implementation of MyPartialClass that contains only a few methods, without needing to wrestle with the potentially long and complex listings that are often found in auto-generated code.

The rules governing partial methods state that they:

  1. Must be declared inside a partial class.
  2. Must contain the keyword partial followed by the keyword void.
  3. Cannot be marked as extern
  4. Can be marked static or unsafe
  5. Can be generic
  6. Can have ref parameters, but not out parameters
  7. Cannot be referenced as a delegate until they are implemented
  8. Cannot have access modifiers such as public, private or internal. Partial methods are implicitly marked as private. This means they cannot be called from outside the partial class, and cannot be declared as virtual.

Since they are primarily designed as a tool for use with auto-generated code, it is unlikely that most developers will ever design and write both halves of a partial method. However, the SQL Designer does make heavy use of partial methods, so many LINQ developers will want to understand this technology.

kick it on DotNetKicks.com