Skip to main content

How to Resolve Error "CS0764: Both partial member declarations must be unsafe or neither may be unsafe" in C#

The Compiler Error CS0764 is a safety consistency error regarding Partial Methods. The message reads: "Both partial member declarations must be unsafe or neither may be unsafe".

In C#, the unsafe keyword allows a method to use pointers and raw memory addresses. Because this changes the security context and verification rules of the method, the compiler requires that if a partial method is defined as unsafe, its implementation must also be explicitly marked as unsafe. You cannot have a "Safe" definition matched with an "Unsafe" implementation, or vice versa.

This guide explains how to align the safety modifiers of your partial methods.

Understanding the Unsafe Mismatch​

When you define a partial method, you are creating a single logical method split into two parts. The attributes and modifiers of that method must be consistent.

  • Definition: partial void Process(int* ptr);
  • Implementation: partial void Process(int* ptr) { ... }

If one part is marked unsafe and the other is not, the compiler sees a contradiction. Even if the parameter types (like int*) imply unsafeness, the unsafe keyword itself must be present on both declarations to satisfy the language rules.

Scenario: Inconsistent unsafe Modifier​

This error commonly occurs when working with P/Invoke or high-performance code where pointers are required. You might define the method correctly in one file but forget the keyword when implementing it in the other.

Example of error​

File 1 (Definition):

public partial class NativeWrapper
{
// Defined as unsafe because it takes a pointer
unsafe partial void OnMemoryMapped(int* address);
}

File 2 (Implementation):

public partial class NativeWrapper
{
// ⛔️ Error CS0764: Both partial member declarations must be unsafe or neither...
// The implementation uses 'int*' but is missing the 'unsafe' modifier.
partial void OnMemoryMapped(int* address)
{
// ...
}
}

Solution 1: Add unsafe to Both (Enable Pointers)​

If the method requires pointers (as in the example above), you must explicitly mark both the definition and the implementation as unsafe.

File 2 (Fixed Implementation):

public partial class NativeWrapper
{
// ✅ Correct: Added 'unsafe' to match File 1.
unsafe partial void OnMemoryMapped(int* address)
{
// Logic handling raw memory
*address = 0xFF;
}
}
note

Project Configuration: Remember that using unsafe requires enabling "Allow unsafe code" in your project settings (<AllowUnsafeBlocks>true</AllowUnsafeBlocks> in .csproj).

Solution 2: Remove unsafe from Both (Managed Code)​

If the method does not actually use pointers or unmanaged memory, and the unsafe keyword was added by mistake or leftover from refactoring, remove it from both declarations.

File 1 (Fixed Definition):

public partial class NativeWrapper
{
// ✅ Correct: Removed 'unsafe'. Using managed types (e.g., IntPtr or ref).
partial void OnMemoryMapped(ref int value);
}

File 2 (Fixed Implementation):

public partial class NativeWrapper
{
// ✅ Correct: Matching safe implementation.
partial void OnMemoryMapped(ref int value)
{
value = 100;
}
}

Conclusion​

CS0764 ensures that the security context of a method is unambiguous.

  1. Check the Definition: Look at the declaration ending in ;. Is it unsafe?
  2. Check the Implementation: Look at the body { ... }. Is it unsafe?
  3. Align Them: Ensure the unsafe keyword is present in both locations or neither.