You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A minimal repro, with source-code provided, is ideal. Using sharplab is preferred for compiler/language issues whenever possible.
Diagnostic Id:
IDE0028 Collection initialization can be simplified
CS0029 Cannot implicitly convert type 'System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, int>>' to 'System.Collections.Generic.KeyValuePair<string, int>' ClassLibrary2 D:\repos\WinFormsApp1\ClassLibrary2\Class1.cs
Expected Behavior:
something like
Dictionary<string,int>dict=[.. GetPairs()];
Actual Behavior:
IDE0028 is very eager to find "...Add(...)" Methods, however does not care if it produces valid code.
Collection Initalizer cannot handle Dictionaries/KeyValuePairs very well (see just above)
Notes:
we've experienced the same Problem with Lists/Collections (ie Generics with Arity=1), however I'm unable to get a working minimal example:
(in this case ExecuteSync() returns an ICollection<string?>, but we know it's exactly one element so capacity=2)
corrected (and working):
The text was updated successfully, but these errors were encountered:
sharwell
changed the title
Collection Initializer cannot handle Extensions akin to IEnumarable<T>.Add(IEnumerable<T>) ( IDE0028 vs CS0029)
Collection Initializer cannot handle Extensions akin to IEnumerable<T>.Add(IEnumerable<T>) ( IDE0028 vs CS0029)
Mar 8, 2024
This is also covered in #71240. Collection expressions do not behave the same way as collection initializers (they are significantly restricted relative to them).
Version Used:
Visual Studio: Version 17.9.2
dotnet: 6.0.419
Steps to Reproduce:
invoke CodeFix

get compiler error

A minimal repro, with source-code provided, is ideal. Using sharplab is preferred for compiler/language issues whenever possible.
Diagnostic Id:
IDE0028
Collection initialization can be simplifiedCS0029
Cannot implicitly convert type 'System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, int>>' to 'System.Collections.Generic.KeyValuePair<string, int>' ClassLibrary2 D:\repos\WinFormsApp1\ClassLibrary2\Class1.csExpected Behavior:
something like
Actual Behavior:
Notes:
we've experienced the same Problem with Lists/Collections (ie Generics with Arity=1), however I'm unable to get a working minimal example:

(in this case
ExecuteSync()
returns anICollection<string?>
, but we know it's exactly one element so capacity=2)corrected (and working):

The text was updated successfully, but these errors were encountered: