Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A partial method is a method declared within a partial type (class, struct, interface, or record) where the method signature is defined in one part of the type, and its implementation is provided in another part. The partial keyword instructs the compiler to merge the signature and the implementation during the compilation process.
using System;

// File1.cs - Declaration
public partial class DataProcessor
{
    // Method signature
    partial void InitializeProcessing();

    public void Start()
    {
        InitializeProcessing(); // Invocation site
    }
}

// File2.cs - Implementation
public partial class DataProcessor
{
    // Method implementation
    partial void InitializeProcessing()
    {
        Console.WriteLine("Initialization complete.");
    }
}

Compiler Behavior and Rules

The rules governing partial methods depend entirely on whether an explicit access modifier is applied to the method signature. C# 9.0 introduced “Extended Partial Methods,” creating two distinct sets of constraints.

1. Implicitly Private Partial Methods (Pre-C# 9.0 Behavior)

If a partial method is declared without an explicit access modifier, it is subject to strict limitations:
  • Access Level: It is implicitly private.
  • Return Type: It must return void.
  • Parameters: It cannot contain out parameters (though ref and in are permitted).
  • Modifiers: It cannot be marked as virtual, override, sealed, new, or extern.
  • IL Erasure: If no implementation is provided in any part of the partial type, the compiler completely removes the method signature and all invocation sites from the emitted Intermediate Language (IL). No runtime overhead is incurred for unimplemented partial methods.

2. Extended Partial Methods (C# 9.0+)

If a partial method is declared with an explicit access modifier (including private), the compiler treats it as an extended partial method, lifting the strict constraints but enforcing implementation:
  • Access Level: Can use any access modifier (public, internal, protected, private, etc.).
  • Return Type: Can return any type, including void.
  • Parameters: Can contain out parameters.
  • Mandatory Implementation: Because the method might return a value, have out parameters, or be invoked externally, the compiler requires an implementation. If the implementation is missing, the compiler throws a build error (CS8795).
// File1.cs - Declaration with access modifier and return type
public partial class DataProcessor
{
    // Because it is public and returns a string, it MUST be implemented
    public partial string GetProcessorState(out int statusCode);
}

// File2.cs - Implementation
public partial class DataProcessor
{
    public partial string GetProcessorState(out int statusCode)
    {
        statusCode = 200;
        return "Active";
    }
}

Signature Matching Requirements

When providing the implementation for a partial method, the structural signature must exactly match the declaration. This includes:
  • Method name.
  • Return type.
  • Access modifiers.
  • Parameter types and modifiers (ref, in, out, params).
  • The number of generic type parameters and their constraints.
Naming Exceptions: Parameter names and generic type parameter names do not need to match between the declaration and the implementation. The names defined in the defining declaration are exposed to callers (e.g., for named arguments), while the names defined in the implementation are used locally within the method body. Attributes applied to the method declaration and the method implementation are merged by the compiler into a single attribute set in the final compiled assembly.
Master C# with Deep Grasping Methodology!Learn More