Skip to content

Commit 5561d4b

Browse files
CopilotBillWagnergewarren
authored
Update CS0460 documentation to include C# 8/9 constraint exceptions (#46528)
* Initial plan for issue * Update CS0460 documentation to include C# 8/9 constraint exceptions Co-authored-by: BillWagner <[email protected]> * Address PR feedback: Update metadata, revert verbatim error message, fix code sample Co-authored-by: BillWagner <[email protected]> * Address PR review feedback: improve clarity and format code comments 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 4fcc57a commit 5561d4b

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

docs/csharp/misc/cs0460.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
description: "Compiler Error CS0460"
33
title: "Compiler Error CS0460"
4-
ms.date: 07/20/2015
4+
ms.date: 06/03/2025
5+
ai-usage: ai-generated
56
f1_keywords:
67
- "CS0460"
78
helpviewer_keywords:
@@ -12,11 +13,16 @@ ms.assetid: 98d39ded-d3f9-4520-b912-892e574c056b
1213

1314
Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
1415

15-
When a generic method that is part of a derived class overrides a method in the base class, you may not specify constraints on the overridden method. The override method in the derived class inherits its constraints from the method in the base class.
16+
When a generic method that is part of a derived class overrides a method in the base class, you can't specify constraints on the overridden method. The override method in the derived class inherits its constraints from the method in the base class.
17+
18+
However, there are exceptions to this rule:
19+
20+
- Starting with C# 9, you can apply the `default` constraint to `override` and explicit interface implementation methods to resolve ambiguities with nullable reference types.
21+
- Starting with C# 8, you can explicitly specify `where T : class` and `where T : struct` constraints on `override` and explicit interface implementation methods to allow annotations for type parameters constrained to reference types.
1622

1723
## Example
1824

19-
The following sample generates CS0460.
25+
The following sample generates CS0460 when attempting to redeclare inherited constraints.
2026

2127
```csharp
2228
// CS0460.cs
@@ -30,12 +36,22 @@ interface I
3036
{
3137
void F1<T>() where T : BaseClass;
3238
void F2<T>() where T : struct;
33-
void F3<T>() where T : BaseClass;
39+
void F3<T>();
40+
void F4<T>() where T : struct;
3441
}
3542

3643
class ExpImpl : I
3744
{
38-
void I.F1<T>() where T : BaseClass {} // CS0460
39-
void I.F2<T>() where T : class {} // CS0460
40-
}
45+
// CS0460 - cannot redeclare inherited constraint.
46+
void I.F1<T>() where T : BaseClass { }
47+
48+
// Allowed - explicit constraint for struct.
49+
void I.F2<T>() where T : struct { }
50+
51+
// Valid since C# 8 - explicit class constraint for nullable annotations.
52+
void I.F4<T>() where T : struct { }
53+
54+
// Valid since C# 9 - default constraint to resolve ambiguities.
55+
void I.F3<T>() where T : default { }
56+
}
4157
```

0 commit comments

Comments
 (0)