Newbie question: single/multiple method arguments
-
Stupid question, I admit, but I'm quite new to C#: how can I write a method which accepts a single input parameter or a collection (e.g. array) of this parameter? For example, let's say I have the method
void MyMethod(int[] args)
. If I try to call the function with a single argument, likeMyMethod(1234)
, the compiler obviously throws an error (int != int[]). Overloading surely solves the issue (i.e. defining bothvoid MyMethod(int[] args)
andvoid MyMethod(int arg)
and factoring out the common behaviour in a different private method) but is there a simpler way to do this? Regards, Andrea -
Stupid question, I admit, but I'm quite new to C#: how can I write a method which accepts a single input parameter or a collection (e.g. array) of this parameter? For example, let's say I have the method
void MyMethod(int[] args)
. If I try to call the function with a single argument, likeMyMethod(1234)
, the compiler obviously throws an error (int != int[]). Overloading surely solves the issue (i.e. defining bothvoid MyMethod(int[] args)
andvoid MyMethod(int arg)
and factoring out the common behaviour in a different private method) but is there a simpler way to do this? Regards, Andrea -
I think I've just found the answer by myself:
void MyMethod(params int[])
. It was a stupid question, indeed! Bye, Andreathat was not a stupid question, it was just simple, sometimes you want to find something that you can describe it to a person but can not describe it to a search engine
I Wish the Life Had CTRL-Z Wizard's First Rule : People are fool,they believe what they want to believe or what they afraid to believe www.subaitech.blogspot.com
-
I think I've just found the answer by myself:
void MyMethod(params int[])
. It was a stupid question, indeed! Bye, AndreaYes, that's how I'd go, but you could also overload the method, have both methods. Oh, of course, you said that. What I should add, is that with overloading, you don't need to "factor out to a private method", you could simply have one method call the other:
void MyMethod(int[] args) { ... }
void MyMethod(int arg) { MyMethod ( new int[] { arg } ) ; }
And leave all the functionality in the one main method. I rarely have to resort to a private method.
-
I think I've just found the answer by myself:
void MyMethod(params int[])
. It was a stupid question, indeed! Bye, AndreaMetal76 wrote:
It was a stupid question, indeed!
Not at all, the solution is not very obvious. It might not be the entire solution, either. You may want to overload that method with one that takes a single
int
, that way you can call it without having the compiler generate code that creates an array object every time (for the params array). Perhaps also add overloads that takes two and three parameters. Thestring.Concat
method is overloaded in that way, for example. Another overload that you may want to add is one that takes anIEnumerable<int>
, that will work for anint[]
, but also for other collections, like aList<int>
.Despite everything, the person most likely to be fooling you next is yourself.