Partial Methods in C#

Last Updated : 17 Sep, 2025

In C#, a partial method is a special kind of method that can be declared inside a partial class or partial struct. It allows you to define a method signature in one part of the class and optionally implement it in another part.

If no implementation is provided, the compiler removes the declaration at compile time, meaning no extra overhead is added. This makes partial methods useful in scenarios where a method might or might not be required, especially in code generation.

Syntax

partial void method_name{
// Code
}

Key Points

  • Declared with the partial keyword.
  • Must exist inside a partial class or partial struct.
  • Can have a declaration (signature) and an optional implementation.
  • If no implementation is provided, the compiler removes the declaration.
  • Must return void (cannot have a return type other than void).
  • Cannot have access modifiers (public, private, etc.).
  • Can be static (in C# 9.0 and later) but cannot be virtual, abstract, override, extern or async.

Example 1: Partial Method with Implementation

C#
using System;

partial class Student
{
    private string name;

    // Partial method declaration
    partial void OnNameChanged();

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnNameChanged(); // call partial method
        }
    }
}

partial class Student
{
    // Partial method implementation
    partial void OnNameChanged()
    {
        Console.WriteLine("Name has been changed to: " + name);
    }
}

class Program
{
    static void Main()
    {
        Student s = new Student();
        s.Name = "Alex";
    }
}

Explanation:

  • OnNameChanged() is declared in one part and implemented in another.
  • When Name is updated, the partial method executes.
  • If no implementation was provided, the compiler would remove the call, so there would be no effect.

Example 2: Partial Method without Implementation

C#
partial class Logger
{
    // Declaration only
    partial void Log(string message);
}

partial class Logger
{
    public void Write()
    {
        // Call is removed by compiler since Log is not implemented
        Log("Message");
    }
}
  • Log is declared but never implemented.
  • At compile time, both the method declaration and its call are removed.
  • This means zero runtime cost.

Use Cases of Partial Methods

  • Code Generation: In tools like Visual Studio, designers can generate partial classes with method declarations that developers may implement later.
  • Optional Logic: Allows optional custom behavior without forcing implementation.
  • Separation of Concerns: Keeps auto-generated and user-written logic separate while still allowing integration.

Benefits

  • No runtime overhead if not implemented.
  • Supports extensibility in generated code.
  • Keeps large classes cleaner and more maintainable.

Restrictions

  • Must return void.
  • Cannot have out parameters.
  • Cannot have access modifiers.
  • Must be inside a partial class or partial struct.
Comment
Article Tags:

Explore