dotnet / dotnet/docs

Advice on CA2225 and operator naming in the face of preexisting conversion methods

Open
#22,045 0 comments 0 reactions 0 assignees View on GitHub
dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
19h 10m
Merged PRs (30d)
268

Description

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

This page should include advice on dealing with this rule with a class that implements preexisting methods
that are alternatives to operators, but have different names than suggested. In my case, this issue occurs with [classes](https://github.com/peteroupc/Numbers/tree/master/Numbers/PeterO/Numbers) named EInteger, EDecimal, EFloat, and ERational.

1. If a class implements, say, the `&` operator, but has an alternative method named `And` (not `BitwiseAnd` as suggested), and that method occurs in shipped code, should CA2225 be suppressed? Or should the class include a method with the suggested name anyway?

2. If a class implements multiple versions of a method that converts to the same data type, each serving a different purpose but not having the suggested name (e.g., the class has `ToInt32Checked`, `ToInt32Unchecked`, and `ToInt32IfExact`, but not `ToInt32` because the name doesn't make its behavior obvious enough), and also has an explicit operator converting to that data type, should CA2225 be suppressed? Or should the class include a method with the suggested name anyway despite the disadvantages of that name compared to the names it does implement?

---
#### 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.