Open Mikejo5000 opened 3 years ago
@koszeggy I'm a bit confused. According to this, you cannot overload the range operator. Are you overloading the range operator when you get the CA2225 warning?
@gewarren Sorry for the confusion. The example is not about the C# 8 range operator feature and its similarly named non-generic Range
struct but a custom type, which happens to be called Range<T>
.
And here are the mentioned operators defined.
And then consider the following example:
public class SomeClass
{
public Range<double> ValidRange { get; set; }
}
Now with the operators the property can be set using the instance.ValidRange = 10.5
or instance.ValidRange = (1.5, 6.25)
syntax but the operators trigger the CA2225 warning. My concern is that though the rule could be satisfied by a generic extension method for literally any object and two-element tuples it would be a supobtimal solution.
I see, these are user-defined conversion operators. @mavasani Do you think the text should be updated to:
In general, do not suppress a warning from this rule if you're implementing a shared library. However, for user-defined conversion operators on generic types, it's okay to suppress the warning instead of implementing the corresponding To<YourType>(this T)
named-conversion method.
This issue has been automatically closed due to no response from the original author. Please feel free to reopen it if you have more information that can help us investigate the issue further.
Reopening because the actual OP did respond.
Ping @mavasani
Moved issue to .NET Old issue: 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:
As
Range<T>
is generic the "best" solution to satisfy the rule would be twoToRange
extension methods on anyT
and onValueTuple<T, T>
, respectively. But that would introduce aToRange
on every object, which is insane.Not mentioning that the main purpose of the operator is to spare typing
new Range<T>(low, high)
and use simply(low, high)
orhigh
in place of anyRange<T>
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.