Introduction to OOP Chapter 15: Overloading : next previous audio real text

Arbitrary Number of Arguments in C#

The language C# has an interesting way to include arbitrary number of arguments:

class ParamsExample {
	public void Write (int x) {
		// use this with one argument
		WriteString("Example one "); 
		WriteString(x.ToString());
	}

	public void Write (double x, int y) {
		// use this with two arguments
		WriteString("Example two ");
		WriteString(x.ToString()); 
		WriteString(y.ToString());
	}

	public void Write (params object [ ] args) {
		// use this with any other combination 
		WriteString("Example three ");
		for (int i = 0; i < args.GetLength(0); i++)
			WriteString(args[i].ToString());
	}
}


	ParamsExample p;
	p.Write(42);
Example one 42
	p.Write(3.14,159);
Example two 3.14159
	p.Write(1,2,3,4);
Example three 1234
	p.Write(3.14);
Example three 3.14
	p.Write(3,"abc");
Example three 3abc

Intro OOP, Chapter 15, Slide 14