Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions docs/csharp/misc/cs0460.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: "Compiler Error CS0460"
title: "Compiler Error CS0460"
ms.date: 07/20/2015
ms.date: 01/02/2025
f1_keywords:
- "CS0460"
helpviewer_keywords:
Expand All @@ -10,13 +10,17 @@
---
# Compiler Error CS0460

Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly, except in specific cases

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.

However, there are exceptions to this rule:
- Starting with C# 9, you can apply the `default` constraint to override and explicit interface implementation methods to resolve ambiguities with nullable reference types.

Check failure on line 18 in docs/csharp/misc/cs0460.md

View workflow job for this annotation

GitHub Actions / lint

Lists should be surrounded by blank lines

docs/csharp/misc/cs0460.md:18 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- Starting with C# 9, you can ..."] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md032.md

Check failure on line 18 in docs/csharp/misc/cs0460.md

View workflow job for this annotation

GitHub Actions / lint

Unordered list indentation

docs/csharp/misc/cs0460.md:18:1 MD007/ul-indent Unordered list indentation [Expected: 0; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md007.md

Check failure on line 18 in docs/csharp/misc/cs0460.md

View workflow job for this annotation

GitHub Actions / lint

Lists should be surrounded by blank lines

docs/csharp/misc/cs0460.md:18 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- Starting with C# 9, you can ..."] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md032.md

Check failure on line 18 in docs/csharp/misc/cs0460.md

View workflow job for this annotation

GitHub Actions / lint

Unordered list indentation

docs/csharp/misc/cs0460.md:18:1 MD007/ul-indent Unordered list indentation [Expected: 0; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md007.md
- 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.

Check failure on line 19 in docs/csharp/misc/cs0460.md

View workflow job for this annotation

GitHub Actions / lint

Unordered list indentation

docs/csharp/misc/cs0460.md:19:1 MD007/ul-indent Unordered list indentation [Expected: 0; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md007.md

Check failure on line 19 in docs/csharp/misc/cs0460.md

View workflow job for this annotation

GitHub Actions / lint

Unordered list indentation

docs/csharp/misc/cs0460.md:19:1 MD007/ul-indent Unordered list indentation [Expected: 0; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md007.md

## Example

The following sample generates CS0460.
The following sample generates CS0460 when attempting to redeclare inherited constraints.

```csharp
// CS0460.cs
Expand All @@ -30,12 +34,19 @@
{
void F1<T>() where T : BaseClass;
void F2<T>() where T : struct;
void F3<T>() where T : BaseClass;
void F3<T>();
void F4<T>() where T : struct;
}

class ExpImpl : I
{
void I.F1<T>() where T : BaseClass {} // CS0460
void I.F2<T>() where T : class {} // CS0460
void I.F1<T>() where T : BaseClass {} // CS0460 - cannot redeclare inherited constraint
void I.F2<T>() where T : struct {} // CS0460 - cannot redeclare inherited constraint
// Valid since C# 8 - explicit class constraint for nullable annotations
void I.F4<T>() where T : class {} // OK - explicit constraint for nullable annotations
// Valid since C# 9 - default constraint to resolve ambiguities
void I.F3<T>() where T : default {} // OK - default constraint
}
```
Loading