On-the-Fly Collections/Dictionaries (params IEnumerable v2) #2992
Replies: 5 comments
-
That syntax is in conflict with the existing initalizer syntax: var e = new Employee
{
Name = "Doe, John",
NickNames = { "Johnny", "Jojo" }
}; |
Beta Was this translation helpful? Give feedback.
-
@quinmars - |
Beta Was this translation helpful? Give feedback.
-
@TonyValenti the initializer syntax does not create a new collection, it initializes an existing one, and that example will almost certainly null ref if NickNames is null. It would be very confusing if in one area |
Beta Was this translation helpful? Give feedback.
-
If you add using System;
using System.Collections.Generic;
using System.Linq;
public class C {
public static void Main() {
Enumerate (new int[] {1, 2, 3});
Enumerate (new [] {4, 5, 6});
Enumerate (new string[] {"a", "b", "c"});
Enumerate (new [] {"d", "e", "f"});
Enumerate2 (new [] {4, 5, 6}, new [] {"d", "e", "f"});
}
public static void Enumerate<T> (IEnumerable<T> seq) {
foreach (var t in seq)
Console.WriteLine ($"{t.GetType ()}: {t}");
}
public static void Enumerate2<T1,T2> (IEnumerable<T1> seq1, IEnumerable<T2> seq2) {
foreach (var t in seq1.Zip (seq2))
Console.WriteLine ($"{t.GetType ()}: {t}");
}
} output:
|
Beta Was this translation helpful? Give feedback.
-
This should be covered by target-typed new(#100). Foo(new List<int>{ 1, 2, 3, 4, 5 }); It's seamless to use this: Foo(new { 1, 2, 3, 4, 5}); A |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Here's what I would like to be able to do:
I propose that syntax like
{1,2,3,4,5}
be shorthand for "Create a new Collection of the given type.The "given type" of the collection should be as follows:
2.a.
var x = {1,2,3,4,5}; //array of Best common type. In this case, int[]
2.b.
double[] x = {1,2,3,4,} //array of double. Target Typing
2.c.
With syntax like this, I it removes the need for any work to be done on improving "params" because it is so easy to just type the curly braces and list all the values out that way. It also basically makes it possible to do 'Params Anywhere' because you can do:
Beta Was this translation helpful? Give feedback.
All reactions