Collection Expressions: Implicity type ins't supported. #8446
-
I don't understand the purpose of the collections expression, if even when the type is implicitly defined it still causes a compilation error See an example below, an array of only using System;
using System.Text;
// It's obvious, it's an array of `System.Text.Encoding` so the compiler can't understand that?
var encodings = [
Encoding.ASCII,
Encoding.UTF8,
Encoding.Latin1,
Encoding.BigEndianUnicode,
Encoding.Unicode
];
foreach(var enc in encodings){
Console.WriteLine("Name={0}; Web Name={1}", enc.BodyName, enc.WebName);
} Instead: // This makes the code look ugly, you need to declare the
// type explicitly when the type is already obvious.
Encoding[] encodings = [
Encoding.ASCII,
Encoding.UTF8,
Encoding.Latin1,
Encoding.BigEndianUnicode,
Encoding.Unicode
]; In case of mixed types defined, interpret as // Mixed content, will be treated as `Object[]`
var context = [
"my string",
1234,
false
]; |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
Natural typing for collection expressions is something that is planned for C# 13/14. The issue isn't determining the element type, it was settling on the best option for the collection type itself depending on the situation. See: #7913 |
Beta Was this translation helpful? Give feedback.
-
It's definitely not clear to me :-) Should it be an array, a span, a list, or something else entirely?
And what about:
? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The feature was designed with the reality of C# collections in mind, which is that there isn't one array-like collection that rules them all. There's a mix of |
Beta Was this translation helpful? Give feedback.
Natural typing for collection expressions is something that is planned for C# 13/14. The issue isn't determining the element type, it was settling on the best option for the collection type itself depending on the situation.
See: #7913