Simplify Collection initializer by inferring Type #676
Replies: 12 comments
-
Would this also extend to constructors with parameters? var customers = new List<Customer>()
{
new Customer(1, "Customer 1"),
new Customer(2, "Customer 2"),
new Customer(3, "Customer 3"),
new Customer(4, "Customer 4")
} becomes var customers = new List<Customer>()
{
(1, "Customer 1"),
(2, "Customer 2"),
(3, "Customer 3"),
(4, "Customer 4")
} |
Beta Was this translation helpful? Give feedback.
-
Thinking more on my last comment, consider the following code that is currently valid: public Customer(Customer original) {
// copy constructor
} later: var suzy = new Customer();
var customers = new List<Customer>
{
(suzy)
}; The intention may be to call the copy constructor, but today it simply adds the existing |
Beta Was this translation helpful? Give feedback.
-
@bondsbw Also var customers = new List<Customer>()
{
new {Id=1,Name="Customer 1"},
new {Id=2,Name="Customer 2"},
new (3, "Customer 3"),
new (4, "Customer 4")
} This way, the proposal could be expanded not only for collections, but for any constructor call where the target type is known, e.g. Customer c = new {Id=1,Name="Customer 1"};
c = new (2, "Customer 2"); |
Beta Was this translation helpful? Give feedback.
-
Related to #100. |
Beta Was this translation helpful? Give feedback.
-
So we will have a whole new class of breaking change when adding overloads 😁 |
Beta Was this translation helpful? Give feedback.
-
@bondsbw your constructor example would also not work with tuples, e.g.: public class Customer
{
public Customer(int a, string b) { ... }
public static implicit operator Customer((int, string) tuple) { ... }
}
List<Customer> list = new List<Customer>
{
(315, "foobar") // constructor or implicit conversion?
}; which is valid code today... |
Beta Was this translation helpful? Give feedback.
-
Collection initializers support adding elements using any public static class CustomerExtensions
{
public static void Add(this ICollection<Customer> collection, int id, string name)
=> collection.Add(new Customer { Id = id, Name = name });
} And it already does what you're asking for: var customers = new List<Customer>()
{
{1, "Customer 1"},
{2, "Customer 2"},
{3, "Customer 3"},
{4, "Customer 4"}
} |
Beta Was this translation helpful? Give feedback.
-
@paulomorgado That's a lot of boilerplate for someone to write just to get this slightly simpler syntax though. |
Beta Was this translation helpful? Give feedback.
-
Judging by the discussion above, is it really simpler? |
Beta Was this translation helpful? Give feedback.
-
@zippec 's suggestion that this be an extension of target typed new expressions makes the most sense to me. Though I agree the savings is minimal either way. |
Beta Was this translation helpful? Give feedback.
-
As @Joe4evr pointed out, with #100, you'd be able to do this: var customers = new List<Customer>
{
new() {Id=1,Name="Customer 1"},
new() {Id=2,Name="Customer 2"},
new() {Id=3,Name="Customer 3"},
new() {Id=4,Name="Customer 4"}
}; |
Beta Was this translation helpful? Give feedback.
-
Which is why we need more expressive generic constraints 😁 public static class CollectionExtensions
{
public static void Add<T, A, B>(this ICollection<T> collection, A a)
where T: new(A) => collection.Add(new T(a));
public static void Add<T, A, B>(this ICollection<T> collection, A a, B b)
where T: new(A, B) => collection.Add(new T(a, b));
public static void Add<T, A, B, C>(this ICollection<T> collection, A a, B b, C c)
where T: new(A, B, C) => collection.Add(new T(a, b, c));
} I believe this has been proposed many times. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi There,
It would be really nice if collection initializer can be extended to understand the type.
for e.g.
Instead
Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions