Skip to content

Commit 74706a4

Browse files
CopilotBillWagnergewarren
authored
Define "defining declaration" and enhance CS0759 compiler error documentation (#47628)
* Initial plan * Add detailed explanation and examples for CS0759 compiler error Co-authored-by: BillWagner <[email protected]> * Update target framework to .NET 9 in partial-declarations project Co-authored-by: BillWagner <[email protected]> * Address review feedback: simplify CS0759 explanation and fix code comment punctuation Co-authored-by: gewarren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: gewarren <[email protected]>
1 parent bf6134c commit 74706a4

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,20 @@ 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 method signature without a body). Every partial method with an implementation must have both declarations.
254+
255+
The following example shows code that generates CS0759:
256+
257+
:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="IncorrectExample":::
258+
259+
To fix this error, add the defining declaration:
260+
261+
:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="CorrectExample":::
262+
263+
You can also place both declarations in the same partial class section:
264+
265+
:::code language="csharp" source="./snippets/partial-declarations/CS0759Examples.cs" id="AlternativeCorrect":::
266+
253267
## Partial properties
254268

255269
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)