dotnet / dotnet/docs

False alarm for generic types - CA2225

Open
#22,042 6 comments 0 reactions 0 assignees View on GitHub
dotnet-fundamentals/svc waiting-on-feedback
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

Moved issue to .NET
Old issue: [https://github.com/MicrosoftDocs/visualstudio-docs/issues/3851](https://github.com/MicrosoftDocs/visualstudio-docs/issues/3851), opened by @koszeggy

The _"Do not suppress a warning from this rule"_ part is way too rigorous this way. Consider the following example:

```cs
public static implicit operator Range(T upperBound) => new Range(upperBound);

public static implicit operator Range((T lowerBound, T upperBound) bounds) =>
new Range(bounds.lowerBound, bounds.upperBound);
```

As `Range` is generic the "best" solution to satisfy the rule would be two `ToRange` extension methods on any `T` and on `ValueTuple`, respectively. But that would introduce a `ToRange` on every object, which is insane.

Not mentioning that the main purpose of the operator is to spare typing `new Range(low, high)` and use simply `(low, high)` or `high` in place of any `Range` assignment, which of course would be much bulkier in a `(low, high).ToRange()` form.

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 53069f56-5f5a-0994-be85-804614e985ad
* Version Independent ID: ed31a2f8-7a52-3463-620e-ce79d20d7202
* Content: [CA2225: Operator overloads have named alternates (code analysis) - .NET](https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2225)
* Content Source: [docs/fundamentals/code-analysis/quality-rules/ca2225.md](https://github.com/dotnet/docs/blob/master/docs/fundamentals/code-analysis/quality-rules/ca2225.md)
* Product: **dotnet-fundamentals**
* GitHub Login: @gewarren
* Microsoft Alias: **gewarren**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.