✅ `... void SomeFunction(string parameters...)` function in CSharp

In java you can define functions that take "infinite" parameters (i forgot the name) with the type name... syntax and then when calling the function you could parse arguments like this "A", "B", "C" without a limit to how many you can give it and the argument you get in the function itself is a array is there a way to do the same thing in c#?
10 Replies
Saber
Saber2y ago
params string[] args
TerrificTable55
TerrificTable55OP2y ago
public void Something(int someThing, String someValues...) {
for (String value : someValues)
System.out.println(value);
}

public ...main() {
Something(5, "A", "BV", "C", "D");
}
public void Something(int someThing, String someValues...) {
for (String value : someValues)
System.out.println(value);
}

public ...main() {
Something(5, "A", "BV", "C", "D");
}
im basically looking for this in c# have seen that on stackoverflow, but it gives me this error (i know how to fix it, but i dont want to have to declare an array to parse to the constructor) Constructor 'GetName' has 1 parameter(s) but is invoked with 2 argument(s)
sibber
sibber2y ago
what constructor
Saber
Saber2y ago
what does your code look like
MODiX
MODiX2y ago
Cyberrex#8052
REPL Result: Success
Something(5, "A", "BV", "C", "D");

public void Something(int someThing, params string[] someValues)
{
foreach (string value in someValues)
Console.WriteLine(value);
}
Something(5, "A", "BV", "C", "D");

public void Something(int someThing, params string[] someValues)
{
foreach (string value in someValues)
Console.WriteLine(value);
}
Console Output
A
BV
C
D
A
BV
C
D
Compile: 584.931ms | Execution: 91.082ms | React with ❌ to remove this embed.
TerrificTable55
TerrificTable55OP2y ago
this is (the snippet of) my code my code:
public class Something {

private List<string> some = new List<string>();

public Something(params string[] args) {
this.some.AddRange(args);
}

}
public class Something {

private List<string> some = new List<string>();

public Something(params string[] args) {
this.some.AddRange(args);
}

}
sibber
sibber2y ago
this should compile and it does
Saber
Saber2y ago
leaving things out and not giving the code that actually has the error isn't too helpful
TerrificTable55
TerrificTable55OP2y ago
oh nvm, im stupid i had another one that just took a string array without the params keyword i removed the other constructor and it works now
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