Feedback on Collection Expressions #7666
Replies: 5 comments 10 replies
-
|
I'd love to be able to use collection expressions to type JSON literals. For example, https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/use-dom has this sample code: var forecastObject = new JsonObject
{
["Date"] = new DateTime(2019, 8, 1),
["Temperature"] = 25,
["Summary"] = "Hot",
["DatesAvailable"] = new JsonArray(
new DateTime(2019, 8, 1), new DateTime(2019, 8, 2)),
["TemperatureRanges"] = new JsonObject
{
["Cold"] = new JsonObject
{
["High"] = 20,
["Low"] = -10
}
},
["SummaryWords"] = new JsonArray("Cool", "Windy", "Humid")
};but it would be nice to write it as something like this: JsonObject forecastObject =
[
"Date": new DateTime(2019, 8, 1),
"Temperature": 25,
"Summary": "Hot",
"DatesAvailable": [new DateTime(2019, 8, 1), new DateTime(2019, 8, 2)],
"TemperatureRanges":
[
"Cold": ["High": 20, "Low": -10]
],
"SummaryWords": ["Cool", "Windy", "Humid"]
]; |
Beta Was this translation helpful? Give feedback.
-
Yes and yes to dictionaries and Whilst this will introduce a third way of initialising a dictionary, it will be super-nice syntax and is definitely worth doing in my view. Regarding |
Beta Was this translation helpful? Give feedback.
-
|
I want a way to omit null values from my collection expression. Ie: var Values = [?x, ?..y]; |
Beta Was this translation helpful? Give feedback.
-
|
I'd like to see support for Before: protected static readonly ISet<string> SupportedImageFileMimeTypes
= new HashSet<string>() { "image/jpeg", "image/png", "image/webp" };After: protected static readonly ISet<string> SupportedImageFileMimeTypes
= ["image/jpeg", "image/png", "image/webp"]; |
Beta Was this translation helpful? Give feedback.
-
|
Extension method support (separate from natural type) to allow things like: public static ImmutableArray<T> AsImmutableArray<T>(this ImmutableArray<T> items) => items;
// consumed elsewhere as
var myImmutableArray = [1, 2, 3].AsImmutableArray(); |
Beta Was this translation helpful? Give feedback.


{{title}}
-
C# 12 introduces collection expressions: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#collection-expressions
What else would you like to see and what are the important scenarios to you? Dictionaries? Support for
var(natural types)?Beta Was this translation helpful? Give feedback.
All reactions