Lecture overview -- Keyboard shortcut: 'u'  Previous page: Use of ref and out parameters in OOP -- Keyboard shortcut: 'p'  Next page: Extension Methods -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 27 : 29
Object-oriented Programming in C#
Data Access, Properties, and Methods
Parameter Arrays

Parameter arrays in C# makes it possible to pass many different actual parameters - of the same type - to a single array parameter.

A parameter array is a formal parameter of array type which can absorb zero, one or more actual parameters

  • Parameter array passing

    • Elements of a parameter array are passed by value

    • A parameter array must be the last parameter in the formal parameter list

    • The formal parameter must be a single dimensional array

    • Arguments that follow ordinary value, ref and out parameters are put into a newly allocated array object

    • Arguments are implicitly converted to the element type of the array

/user/normark/oop-csharp-1/sources/c-sharp/method-parameters/by-out-and-params/ex.csThe class A with a DoAdd method - both out and params. This program is explained

A variant of the DoAdd method where all but the first parameters are summed together and returned in the first parameter, which is an out parameter.

/user/normark/oop-csharp-1/sources/c-sharp/method-parameters/by-out-and-params/outputOutput of class A with DoAdd - both out and params.

/user/normark/oop-csharp-1/sources/c-sharp/int-sequence/intseq.csAn integer sequence - use of params in constructor.

It is possible to have a constructor with a parameter array.


Read more about parameter arrays in the text book version of this material.