Collection expressions turn out to be evil in some scenarios. #111342
-
There is a built-in analyzer IDE0300 that suggests replacing public void M(IReadOnlyCollection<string> cards){
// ...
}
public void Invoke()
{
M(new string[] {"1", "2"}); // IDE suggests M(["1", "2"]);
} The IDE suggests replacing it with This becomes a problem if you're using RMI frameworks like Orleans, which will complain that such a type ( So, I’m not sure why the analyzer suggests this replacement if the two forms are not equivalent, and it’s easy to make a mistake. I also don’t understand why Microsoft didn’t consider this edge case when implementing this feature. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The analyzer rule IDE0300 is configurable in two modes:
|
Beta Was this translation helpful? Give feedback.
The analyzer rule IDE0300 is configurable in two modes:
when_types_exactly_match
andwhen_types_loosely_match
. See the documentation at https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0300 .when_types_exactly_match
has the behavior you want. It won't suggest changes to the actual type created.when_types_loosely_match
is set as the default because it's usually beneficial in more cases, but you can turn it off.