I have 2 constructors, accepting different types of arguments:
public someClass(String s) {
// the string is parsed to an int array.
int[] array = doSomething(s);
this(array);
}
public someClass(int[] array) {
doSomethingElse(array);
}
However on the first constructor I get "Method name is expected". Is there a way to have a constructor call another after performing other actions, or is it simply a limitation of C#?
Unless doSomething
is static.
class someClass
{
public someClass(String s)
: this(doSomething(s))
{ }
public someClass(int[] array)
{
doSomethingElse(array);
}
static int[] doSomething(string s)
{
//do something
}
}
You can't do that. But you could write
public SomeClass(string s) : this(doSomething(s)){}
which is perfectly fine, so long as int[] doSomething(string)
is static
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With