-
Notifications
You must be signed in to change notification settings - Fork 1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Championed: Target-typed implicit array creation expression new[]
#2701
Comments
new[]
new[]
This comment has been minimized.
This comment has been minimized.
Would it not work better to relax the array initializer syntax to allow |
I would like if this also included |
I think this proposal can effectively be subsumed into collection literals. You would be able to do the following: KeyValuePair<string, string>[] array = [new("key1", value1), new("key2", value2)]; That solves the array part. KVP is also very special to collection literals so the above could also be shortened to: KeyValuePair<string, string>[] array = ["key1": value1, "key2": value2];
We support collection/dictionary literals even the absence of a concrete collection type :) |
Right, I think it will also work with IEnumerable but only as long as there is a compatible natural type. IEnumerable<T> x = [value]; // ok; but the target-type (T) won't affect type inference in any ways here.
IEnumerable<T> x = []; // error; no natural type? (is there a default natural type when the list is empty?)
IEnumerable<bool?> x = [true, null]; // error; the most-common-type is `bool` and `List<bool>` fails to assign. Relates to dotnet/roslyn#37950 |
All of the above would be legal @alrz |
Great! Though I didn't find anything about a null-aware common type.. does that last example work with |
The last example will work with 'var' if 'best common type' is willing to infer |
Unfortunately, I think it is not. SharpLab |
@RikkiGibson right, that's what i'm saying. We're gated on that being a supported concept for c#. the same applies to existing collections today. |
When we didn't do that in C# 8, we accepted that it would likely never be done. #881 |
I opened a discussion thread here: #7175 In short: It is great if this type of syntax will work in C# 12: Student[] students = [new("John"),new("Jane")]; But we still should have a goal of implementing this feature on the original syntax for the sake of language consistency. Student[] studentsB = new[] { new("John"), new("Jane") }; // This would ideally work as well. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Allow
KeyValuePair<string, string>[] array = new[] { new("key1", value1), new("key2", value2) };
With introduction of
new()
expression (#100), the following:can be simplified to:
but you still need to repeat the type following the field/property initializer. Closest you can get is something like:
For the sake of completeness, I'd suggest to also make
new[]
a target-typed expression,The mechanics of such feature is similar to #2389 or #2460 where there are two source of type inference, the common type and the target-type.
Note: A list/dictionary literal feature would not supersede this proposal because in above examples there's no concrete collection type to infer.
The text was updated successfully, but these errors were encountered: