Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,23 @@ Certain `partial` method declarations don't require an *implementing declaration

When a partial method includes an implementing declaration, both declarations must be identical. Exactly one implementing declaration can be defined.

**CS0759** occurs when you have an implementing declaration (a partial method with a body) but no corresponding defining declaration (the signature without a body). Every partial method with an implementation must have both:

1. A *defining declaration*: the method signature without a body, as mentioned in the [Partial members](#partial-members) section.
2. An *implementing declaration*: the method signature with a body.

The following example shows code that generates CS0759:

:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="IncorrectExample":::

To fix this error, add the defining declaration:

:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="CorrectExample":::

You can also place both declarations in the same partial class section:

:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="AlternativeCorrect":::

## Partial properties

The following errors indicate mistakes in your partial property or indexer declarations:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;

namespace CS0759Examples
{
// <IncorrectExample>
// This will cause CS0759: No defining declaration found for implementing declaration of partial method
// Uncomment the class below to see the CS0759 error:
/*
public partial class ExampleClass
{
// ERROR: This is an implementing declaration without a corresponding defining declaration
partial void MyMethod() // CS0759
{
Console.WriteLine("Implementation without definition");
}
}
*/
// </IncorrectExample>

// <CorrectExample>
// Correct way: Provide both the defining declaration and implementing declaration
public partial class CorrectExampleClass
{
// Defining declaration (signature without body)
partial void MyMethod();
}

public partial class CorrectExampleClass
{
// Implementing declaration (signature with body)
partial void MyMethod()
{
Console.WriteLine("This works correctly");
}
}
// </CorrectExample>

// <AlternativeCorrect>
// Alternative correct approach: defining and implementing in same partial class
public partial class AlternativeExampleClass
{
// Defining declaration
partial void MyMethod();

// Implementing declaration in same partial class section
partial void MyMethod()
{
Console.WriteLine("This also works correctly");
}
}
// </AlternativeCorrect>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>