Skip to main content

How to Resolve Error "CS0762: Cannot create delegate from method because it is a partial method without an implementing declaration" in C#

The Compiler Error CS0762 is a safety restriction regarding Partial Methods. The message reads: "Cannot create delegate from method 'MethodName' because it is a partial method without an implementing declaration".

In C#, standard partial methods are designed to be optional. If you define a partial method but do not provide an implementation (a body) in any file, the compiler removes all calls to that method from the final compiled code.

However, creating a Delegate (like Action, Func, or subscribing to an event) requires a physical memory address of a method to point to. You cannot create a pointer to a method that might be deleted by the compiler. Therefore, C# forbids creating delegates from partial methods unless the compiler can guarantee the method exists.

This guide explains how to ensure your partial methods are valid targets for delegates.

Understanding Partial Method Removal

Consider this standard partial method: partial void OnLoaded();

  • Direct Call: OnLoaded(); -> Allowed. If OnLoaded is not implemented, the compiler simply deletes this line. No harm done.
  • Delegate Creation: Action a = OnLoaded; -> Error CS0762. If OnLoaded is not implemented, the variable a would point to null or invalid memory, crashing the app when invoked later.

Scenario 1: Assigning to Delegates or Events

This error commonly occurs when you try to use a partial method as an event handler or pass it as a callback.

Example of error

File 1 (Definition):

public partial class Controller
{
// A standard partial method (implicitly private, returns void)
partial void OnClick();

public void Setup()
{
// ⛔️ Error CS0762: Cannot create delegate from method 'OnClick'...
// The compiler doesn't know if 'OnClick' will actually exist in the compiled DLL.
Action callback = OnClick;
}
}

Solution 1: Provide the Implementing Declaration

The simplest fix is to ensure the method actually has a body. Once the compiler sees the implementation, it knows the method will not be removed, so creating a delegate becomes safe.

File 2 (Implementation):

public partial class Controller
{
// ✅ Correct: Providing the body makes the method 'real'.
// The error in File 1 will disappear because the method now has an address.
partial void OnClick()
{
System.Console.WriteLine("Clicked!");
}
}
warning

If you delete this implementation later, the error in File 1 will return.

Solution 2: Use a Wrapper Method (Proxy Pattern)

If you want to keep the partial method optional (meaning you might not implement it), you cannot use it directly as a delegate. Instead, create a standard private method that calls the partial method.

You can create a delegate to the Wrapper (which always exists), and the Wrapper calls the Partial (which might be removed).

public partial class Controller
{
partial void OnClick();

// 1. Create a non-partial wrapper
private void OnClickWrapper()
{
// Calling the partial method directly is allowed.
// If OnClick is missing, this line vanishes, and the wrapper does nothing.
OnClick();
}

public void Setup()
{
// 2. Create the delegate pointing to the Wrapper
// ✅ Correct: 'OnClickWrapper' is guaranteed to exist.
Action callback = OnClickWrapper;
}
}

Scenario 2: Using C# 9.0+ Extended Partial Methods

In modern C#, partial methods evolved. If you add an access modifier (like private, internal, public) to a partial method, it becomes an Extended Partial Method.

Rules for Extended Partial Methods:

  1. They must have an implementation (it is no longer optional).
  2. Because they definitely exist, the compiler never removes them.
  3. Because they are never removed, delegates are allowed.

Example of error (Standard Partial)

public partial class Service
{
// No access modifier = Standard (Optional) = No Delegates
partial void Log();
}

Solution: Add an Access Modifier

By explicitly adding private, you opt-in to the new behavior.

File 1 (Definition):

public partial class Service
{
// ✅ Correct: Adding 'private' makes implementation mandatory.
// Delegates are now allowed.
private partial void Log();

public void Initialize()
{
Action logger = Log; // Valid!
}
}

File 2 (Implementation):

public partial class Service
{
// You are now REQUIRED to implement this.
private partial void Log()
{
// ...
}
}

Conclusion

CS0762 prevents runtime crashes caused by pointing to non-existent code.

  1. Check Implementation: Does the partial method have a body { ... } in another file? If not, you can't make a delegate from it.
  2. Use a Wrapper: If the method is truly optional, wrap the call OnPartial() inside a standard private void Wrapper().
  3. Upgrade Syntax: Use C# 9.0 Extended Partial Methods (private partial void) if you want to enforce implementation and allow delegates natively.