Skip to content

Document invariant validity positions and remediation for CS1961 (Fixes #32014) - #56685

Open
Manish-1kumar wants to merge 3 commits into
dotnet:mainfrom
Manish-1kumar:users/manish/issue-32014-cs1961-variance-documentation
Open

Manish-1kumar wants to merge 3 commits into
dotnet:mainfrom
Manish-1kumar:users/manish/issue-32014-cs1961-variance-documentation

Conversation

@Manish-1kumar

@Manish-1kumar Manish-1kumar commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Addresses issue #32014 by expanding the diagnostic documentation for CS1961 in generic-type-parameters-errors.md:

  • Added all three compiler message variants for CS1961 (covariantly, contravariantly, and invariantly valid).
  • Explained the cause of the invariant validity diagnostic when a variant type parameter is used with an invariant generic collection type (such as ICollection<T> or IList<T>) or in both input and output positions.
  • Provided concrete remediation patterns: removing the variance modifier, replacing invariant collections with covariant read-only interfaces (such as IEnumerable<T> or IReadOnlyCollection<T>), or splitting the interface into covariant and contravariant roles.

Fixes #32014.


Internal previews

File Preview link
docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md Learn preview

Build report

@dotnetrepoman dotnetrepoman Bot added this to the November 2026 milestone Nov 1, 2026
@dotnet-policy-service dotnet-policy-service Bot added okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. dotnet-csharp/svc lang-reference/subsvc csharp-diagnostics/subsvc community-contribution Indicates PR is created by someone from the .NET community. labels Nov 1, 2026
@Manish-1kumar
Manish-1kumar force-pushed the users/manish/issue-32014-cs1961-variance-documentation branch from 975f08e to 2cde785 Compare November 30, 2026 17:40
@BillWagner
BillWagner requested a balanced review from Copilot December 1, 2026 17:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The CS1961 documentation mischaracterizes separate input/output uses as invariant and recommends covariant interfaces without limiting the remedy to compatible variance positions.

2 open issues
What changed in this PR

Expands the C# compiler diagnostic documentation for CS1961 to address the missing invariant-validity guidance reported in #32014.

Changes:

  • Covers covariant, contravariant, and invariant diagnostic wording.
  • Explains variance positions and invariant generic collection types.
  • Adds remediation options: remove variance modifiers, use compatible read-only interfaces, or separate interface roles.
File Description
docs/​csharp/​language-reference/​compiler-messages/​generic-type-parameters-errors.md Expands CS1961 explanations and remediation guidance.

Review effort: Balanced

@BillWagner BillWagner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One small change, and then this is ready.


- **CS1960**: *Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.*
- **CS1961**: *Invalid variance: The type parameter must be covariantly valid on 'type'. 'type parameter' is contravariant.*
- **CS1961**: *Invalid variance: The type parameter '{0}' must be covariantly / contravariantly / invariantly valid on '{1}'. '{0}' is {2}.*

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We never leave the positional strings in the article. We replace them with some appropriate placeholder:

Suggested change
- **CS1961**: *Invalid variance: The type parameter '{0}' must be covariantly / contravariantly / invariantly valid on '{1}'. '{0}' is {2}.*
- **CS1961**: *Invalid variance: The type parameter 'T' must be covariantly / contravariantly / invariantly valid on 'Type1'. 'T' is Type2.*

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have applied this suggestion in 27ba108. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates PR is created by someone from the .NET community. csharp-diagnostics/subsvc dotnet-csharp/svc lang-reference/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visual Studio "error toast" link does not supply valid information

3 participants