Skip to content

Update CS0460 documentation to include C# 8/9 constraint exceptions #46528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 2, 2025

The CS0460 compiler error documentation was outdated and didn't reflect the constraint exceptions introduced in C# 8 and C# 9. The existing documentation stated that constraints cannot be specified directly on override and explicit interface implementation methods, but this is no longer entirely accurate.

Changes Made

  • Updated description to clarify that while constraints are generally inherited, there are specific exceptions
  • Added C# 9 exception for the default constraint, which can be applied to resolve ambiguities with nullable reference types
  • Added C# 8 exception for explicit where T : class and where T : struct constraints to allow annotations for type parameters constrained to reference types
  • Updated example code to demonstrate both the error cases (CS0460) and the valid exception cases
  • Updated ms.date to reflect the documentation update

Example

The updated documentation now shows that while this still generates CS0460:

void I.F1<T>() where T : BaseClass {}   // CS0460 - cannot redeclare inherited constraint

These are now valid since C# 8 and C# 9:

// Valid since C# 8 - explicit class constraint for nullable annotations
void I.F4<T>() where T : class {}       // OK

// Valid since C# 9 - default constraint to resolve ambiguities  
void I.F3<T>() where T : default {}     // OK

Fixes #46009.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/misc/cs0460.md docs/csharp/misc/cs0460

@Copilot Copilot AI changed the title [WIP] Expanding the description of CS0460 Update CS0460 documentation to include C# 8/9 constraint exceptions Jun 2, 2025
@Copilot Copilot AI requested a review from BillWagner June 2, 2025 21:12
Copilot finished work on behalf of BillWagner June 2, 2025 21:13
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @copilot

I have a few suggestions, and then this is ready.

Copilot finished work on behalf of BillWagner June 3, 2025 14:30
@BillWagner BillWagner marked this pull request as ready for review June 3, 2025 14:31
@BillWagner BillWagner requested a review from a team as a code owner June 3, 2025 14:31
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot made all the changes I requested in my review. This is ready for a final human review.

@BillWagner BillWagner requested a review from gewarren June 3, 2025 14:32
@BillWagner BillWagner enabled auto-merge (squash) June 3, 2025 15:31
@gewarren gewarren requested a review from Copilot June 3, 2025 21:40
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the CS0460 error documentation to accurately reflect the exceptions introduced in C# 8 and C# 9 regarding constraint specifications on override and explicit interface implementation methods.

  • Updated metadata (ms.date, ai-usage)
  • Added sections explaining C# 8 and C# 9 exceptions with corresponding sample code
  • Revised example code to differentiate between error and valid usage scenarios

auto-merge was automatically disabled June 3, 2025 21:56

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Jun 3, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • http://168.63.129.16:80/machine/
    • Triggering command: /usr/bin/python3 -u bin/WALinuxAgent-2.13.1.1-py3.9.egg -collect-logs (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of gewarren June 3, 2025 21:57
@BillWagner BillWagner merged commit 5561d4b into main Jun 5, 2025
10 checks passed
@BillWagner BillWagner deleted the copilot/fix-46009 branch June 5, 2025 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expanding the description of CS0460
3 participants