❔ Adding list in single line

In C++, I could add a list in a single line using a vector. For example:
std::vector<std::string> months = { "January", "February", "March" };

// another thing I could do:
setMonths({ "January", "February", "March" });
std::vector<std::string> months = { "January", "February", "March" };

// another thing I could do:
setMonths({ "January", "February", "March" });
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
List<string> months = new List<string>();
months.Add("January");
months.Add("February");
months.Add("March");
List<string> months = new List<string>();
months.Add("January");
months.Add("February");
months.Add("March");
Or AddRange(): -- Which isn't too bad, but still feels bigger than it should be.
List<string> vMonths = new List<string>();
string[] months = new string[] { "January", "February", "March" };
vMonths.AddRange(months);
dt.SetMonths(vMonths);
List<string> vMonths = new List<string>();
string[] months = new string[] { "January", "February", "March" };
vMonths.AddRange(months);
dt.SetMonths(vMonths);
Ideal:
dt.SetMonths({ "January", "February", "March" }); // Param: SetMonths(List<string> list)
dt.SetMonths({ "January", "February", "March" }); // Param: SetMonths(List<string> list)
18 Replies
ero
ero2y ago
collection initializer;
List<string> months = new { "January", "February", "March" };
List<string> months = new { "January", "February", "March" };
anita
anita2y ago
You could create an extension method with a params array. I think he ment add not initialize
triplemocha
triplemochaOP2y ago
I get CS0746 with the strings.
ero
ero2y ago
Yeah i know every C# error off the top of my head
triplemocha
triplemochaOP2y ago
That's good.
ero
ero2y ago
It was a joke
triplemocha
triplemochaOP2y ago
Most just Google CS#.
ero
ero2y ago
The burden of detail is not on me lol It's your problem
MODiX
MODiX2y ago
Ero#1111
REPL Result: Failure
List<string> months = new { "January", "February", "March" };
List<string> months = new { "January", "February", "March" };
Exception: CompilationErrorException
- Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
- Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
- Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
- Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
- Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
- Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
Compile: 493.222ms | Execution: 0.000ms | React with ❌ to remove this embed.
ero
ero2y ago
Oh whoops That is my bad indeed new() But you could have just guessed that yourself
triplemocha
triplemochaOP2y ago
Okay, that worked. Not really. dt.SetMonths(new() { "January", "February", "March" }); I was able to reduce it to this line with my set method.
ero
ero2y ago
Sure So it did work
triplemocha
triplemochaOP2y ago
Yes. Thanks.
anita
anita2y ago
something like this would also work:
dt.SetMonths("January", "February");
public void SetMonths(params string[] months)
dt.SetMonths("January", "February");
public void SetMonths(params string[] months)
Omnissiah
Omnissiah2y ago
i don't know if i'm losing some context but there is List.AddRange([stuff])
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
triplemocha
triplemochaOP2y ago
All good suggestions. Thanks. The params one seems fluid for a user-created class method.
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server