diff --git a/docs/csharp/language-reference/compiler-messages/partial-declarations.md b/docs/csharp/language-reference/compiler-messages/partial-declarations.md index 6563d19b8e1c5..9dbcfd384ebfd 100644 --- a/docs/csharp/language-reference/compiler-messages/partial-declarations.md +++ b/docs/csharp/language-reference/compiler-messages/partial-declarations.md @@ -250,6 +250,20 @@ 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 method signature without a body). Every partial method with an implementation must have both declarations. + +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: diff --git a/docs/csharp/language-reference/compiler-messages/snippets/partial-declarations/CS0759Examples.cs b/docs/csharp/language-reference/compiler-messages/snippets/partial-declarations/CS0759Examples.cs new file mode 100644 index 0000000000000..a954358dce03a --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/partial-declarations/CS0759Examples.cs @@ -0,0 +1,52 @@ +using System; + +namespace CS0759Examples +{ + // + // 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"); + } + } + */ + // + + // + // 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"); + } + } + // + + // + // 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"); + } + } + // +} \ No newline at end of file diff --git a/docs/csharp/language-reference/compiler-messages/snippets/partial-declarations/partial-declarations.csproj b/docs/csharp/language-reference/compiler-messages/snippets/partial-declarations/partial-declarations.csproj new file mode 100644 index 0000000000000..e33d07d003c30 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/partial-declarations/partial-declarations.csproj @@ -0,0 +1,8 @@ + + + + net9.0 + enable + + + \ No newline at end of file