❔ Adding list in single line
In C++, I could add a list in a single line using a vector. For example:
Is there a similar way with C# and lists? That way I'm not calling "Add" 12 times, but keeping them like an array on initialization.
With C#, I think it's just
Or AddRange(): -- Which isn't too bad, but still feels bigger than it should be.
Ideal:
18 Replies
collection initializer;
You could create an extension method with a params array.
I think he ment add not initialize
I get CS0746 with the strings.
Yeah i know every C# error off the top of my head
That's good.
It was a joke
Most just Google CS#.
The burden of detail is not on me lol
It's your problem
Ero#1111
REPL Result: Failure
Exception: CompilationErrorException
Compile: 493.222ms | Execution: 0.000ms | React with ❌ to remove this embed.
Oh whoops
That is my bad indeed
new()
But you could have just guessed that yourselfOkay, that worked.
Not really.
dt.SetMonths(new() { "January", "February", "March" });
I was able to reduce it to this line with my set method.Sure
So it did work
Yes. Thanks.
something like this would also work:
i don't know if i'm losing some context but there is
List.AddRange([stuff])
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
All good suggestions. Thanks. The params one seems fluid for a user-created class method.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.