Is it possible to send a variable number of arguments to a method?
For instance if I want to write a method that would concatenate many string[]
objects into one string, but I wanted it to be able to accept arguments without knowing how many I would want to pass in, how would I do this?
A text editor, like Notepad, to write C code. A compiler, like GCC, to translate the C code into a language that the computer will understand.
First, we will write C and LXXIII in numbers, i.e. C = 100 and LXXIII = 70 + 3 = 73. Now, 100 - 73 = 27. And 27 = XXVII. Therefore, XXVII should be subtracted from C roman numerals to get LXXIII.
Conclusion. In a nutshell, the main difference between C and C++ is that C is a procedural with no support for objects and classes, whereas C++ is a combination of procedural and object-oriented programming languages.
You would do this as:
string ConcatString(params string[] arguments)
{
// Do work here
}
This can be called as:
string result = ConcatString("Foo", "Bar", "Baz");
For details, see params (C# Reference).
FYI - There is already a String.Concat(params object[] args)
- it will concatenate any set of objects by calling ToString() on each. So for this specific example, this is probably not really that useful.
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