From 9152ee916c2ea44a7fd45520af52afc84ed8f3a1 Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski Date: Tue, 22 Jul 2025 23:10:57 +0200 Subject: [PATCH 1/2] Update the CS1031 page and remove not needed code --- docs/csharp/misc/cs1031.md | 50 ++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/docs/csharp/misc/cs1031.md b/docs/csharp/misc/cs1031.md index a0f10410b899f..50e4bb227ebba 100644 --- a/docs/csharp/misc/cs1031.md +++ b/docs/csharp/misc/cs1031.md @@ -12,35 +12,27 @@ ms.assetid: 14196659-aaac-4df2-a4ed-0bebb8097d59 Type expected - A type parameter is expected. - -## Example +A type has not been specified where expected, when [overloading an operator](../language-reference/operators/operator-overloading.md). - The following sample generates CS1031: - -```csharp -// CS1031.cs -namespace x -{ - public class ii - { - } - - public class a +## Example + +The following sample generates CS1031: + +```csharp +namespace x +{ + public class I + { + } + + public class A { - public static operator +(a aa) // CS1031 - // try the following line instead - // public static ii operator +(a aa) - { - return new ii(); - } - - public static void Main() - { - e = new base; // CS1031, not a type - e = new this; // CS1031, not a type - e = new (); // CS1031, not a type - } - } -} + public static operator +(A aa) // CS1031 + // try the following line instead + // public static ii operator +(a aa) + { + return new ii(); + } + } +} ``` From 89f923ac1324494697c52866c7686c0ee214b0d2 Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski Date: Tue, 22 Jul 2025 23:21:38 +0200 Subject: [PATCH 2/2] Improve the code example and emphasize on the type parameter --- docs/csharp/misc/cs1031.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/csharp/misc/cs1031.md b/docs/csharp/misc/cs1031.md index 50e4bb227ebba..c945de1b0607e 100644 --- a/docs/csharp/misc/cs1031.md +++ b/docs/csharp/misc/cs1031.md @@ -13,6 +13,8 @@ ms.assetid: 14196659-aaac-4df2-a4ed-0bebb8097d59 Type expected A type has not been specified where expected, when [overloading an operator](../language-reference/operators/operator-overloading.md). +
Missing *type* in this case means that there's no *type* given for the return type of the overloaded operator. +This error shouldn't be confused with missing [generic type parameter](../../csharp/programming-guide/generics/generic-type-parameters.md). ## Example @@ -27,11 +29,14 @@ namespace x public class A { - public static operator +(A aa) // CS1031 - // try the following line instead - // public static ii operator +(a aa) + public static operator +(A a) // CS1031 - Overloaded operator missing a type { - return new ii(); + return new I(); + } + + public static I operator +(A a) // Correct - type was specified + { + return new I(); } } }