Feedback on Collection Expressions #7666
Replies: 15 comments 83 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.
-
|
Beta Was this translation helpful? Give feedback.
-
ℹ️ Until there is actual support for dictionary literals, the following syntax does work (sharplab) for C# 12 so long as you're willing to declare an extension method: Dictionary<int, string> dict1 = [new(1, "one"), new(2, "two")];
Dictionary<int, string> dict2 = [.. dict1, new(3, "three")];
public static class Extensions
{
public static void Add<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> entry)
{
dictionary.Add(entry);
}
} (Don't worry, this isn't recursive. It binds to |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
A way to create ILookup would be appreciated (probably by returning an instance of the internal Lookup class). Currently, the only way to create them is using the .ToLookup() extension method. ILookup<int, string> x = [ 1 => ["a", "b", "c"], 2 => ["d"] ]; |
Beta Was this translation helpful? Give feedback.
-
Suggestion 1. Integer range expressions within collection expressions, e.g.: int[] array1 = [0..n]; // n elements from 0 inclusive to n exclusive
long[] array2 = [0..10, 20..30, 100]; // 21 elements Here, Suggestion 2 (along with suggestion 1). Collection expressions in foreach (int i in 0..n) { Something(i); }
foreach (long i in 0..10, 20..30, 100) { Something(i); } Of course, this would be a "collection expression" only when there are at least two elements or a range. |
Beta Was this translation helpful? Give feedback.
-
What's the rationale here? Like can a collection expression be assigned to string in any circumstance? |
Beta Was this translation helpful? Give feedback.
-
There seems to be a lack of a default type now, consider the following scenariopublic class A
{
public static implicit operator A(int[] source) => throw new NotImplementedException();
//...
}
// Suppose there's a method accepting an argument of type A
void M(A a) => throw new NotImplementedException();
// Current attempt at calling the method
void M2()
{
M([1,2,3]); // Produces error CS1503
}
// The correct way to call it currently
int[] x1 = {1, 2, 3};
M(x1); // This works correctly |
Beta Was this translation helpful? Give feedback.
-
Have you considered adding features like 'index functions'? If we have such functions, we only need to provide the expression of this function and the length to construct an array in this form before compilation. However, after compilation, it can be an array with a fixed length and each element is also determined. |
Beta Was this translation helpful? Give feedback.
-
I'd love to be able to use collection expression directly in
yields Currently known workarounds:
or
Notes:
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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