C
C#14mo ago
triplemocha

❔ 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
ero14mo ago
collection initializer;
List<string> months = new { "January", "February", "March" };
List<string> months = new { "January", "February", "March" };
chef drone builder
You could create an extension method with a params array. I think he ment add not initialize
triplemocha
triplemocha14mo ago
I get CS0746 with the strings.
ero
ero14mo ago
Yeah i know every C# error off the top of my head
triplemocha
triplemocha14mo ago
That's good.
ero
ero14mo ago
It was a joke
triplemocha
triplemocha14mo ago
Most just Google CS#.
ero
ero14mo ago
The burden of detail is not on me lol It's your problem
MODiX
MODiX14mo 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
ero14mo ago
Oh whoops That is my bad indeed new() But you could have just guessed that yourself
triplemocha
triplemocha14mo 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
ero14mo ago
Sure So it did work
triplemocha
triplemocha14mo ago
Yes. Thanks.
chef drone builder
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)
boiled goose
boiled goose14mo ago
i don't know if i'm losing some context but there is List.AddRange([stuff])
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
triplemocha
triplemocha14mo ago
All good suggestions. Thanks. The params one seems fluid for a user-created class method.
Accord
Accord14mo 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
More Posts