Skip to content

Commit 0dab670

Browse files
CopilotBillWagner
andcommitted
Add detailed explanation and examples for CS0759 compiler error
Co-authored-by: BillWagner <[email protected]>
1 parent e534278 commit 0dab670

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

docs/csharp/language-reference/compiler-messages/partial-declarations.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ Certain `partial` method declarations don't require an *implementing declaration
250250

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

253+
**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:
254+
255+
1. A *defining declaration*: the method signature without a body, as mentioned in the [Partial members](#partial-members) section.
256+
2. An *implementing declaration*: the method signature with a body.
257+
258+
The following example shows code that generates CS0759:
259+
260+
:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="IncorrectExample":::
261+
262+
To fix this error, add the defining declaration:
263+
264+
:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="CorrectExample":::
265+
266+
You can also place both declarations in the same partial class section:
267+
268+
:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="AlternativeCorrect":::
269+
253270
## Partial properties
254271

255272
The following errors indicate mistakes in your partial property or indexer declarations:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
3+
namespace CS0759Examples
4+
{
5+
// <IncorrectExample>
6+
// This will cause CS0759: No defining declaration found for implementing declaration of partial method
7+
// Uncomment the class below to see the CS0759 error:
8+
/*
9+
public partial class ExampleClass
10+
{
11+
// ERROR: This is an implementing declaration without a corresponding defining declaration
12+
partial void MyMethod() // CS0759
13+
{
14+
Console.WriteLine("Implementation without definition");
15+
}
16+
}
17+
*/
18+
// </IncorrectExample>
19+
20+
// <CorrectExample>
21+
// Correct way: Provide both the defining declaration and implementing declaration
22+
public partial class CorrectExampleClass
23+
{
24+
// Defining declaration (signature without body)
25+
partial void MyMethod();
26+
}
27+
28+
public partial class CorrectExampleClass
29+
{
30+
// Implementing declaration (signature with body)
31+
partial void MyMethod()
32+
{
33+
Console.WriteLine("This works correctly");
34+
}
35+
}
36+
// </CorrectExample>
37+
38+
// <AlternativeCorrect>
39+
// Alternative correct approach: defining and implementing in same partial class
40+
public partial class AlternativeExampleClass
41+
{
42+
// Defining declaration
43+
partial void MyMethod();
44+
45+
// Implementing declaration in same partial class section
46+
partial void MyMethod()
47+
{
48+
Console.WriteLine("This also works correctly");
49+
}
50+
}
51+
// </AlternativeCorrect>
52+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)