Lecture overview -- Keyboard shortcut: 'u'  Previous page: Reference Parameters -- Keyboard shortcut: 'p'  Next page: Use of ref and out parameters in OOP -- 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 25 : 29
Object-oriented Programming in C#
Data Access, Properties, and Methods
Output Parameters

out parameters in C# are reference parameters used only for output purposes.

Output parameters are used for output from methods. The method is supposed to assign values to output parameters.

  • Output parameter passing

    • The corresponding argument must be a variable

    • The corresponding argument needs not to have a value on beforehand

    • The formal parameter should/must be assigned by the method

    • The formal parameter becomes another name (an alias) of the argument

    • The types of the formal parameter and the argument must be identical

    • The actual parameter must be prefixed with the keyword out

A formal ref parameter serve as an alias - an alternative name - of the corresponding actual parameter. The formal parameter must be assigned in the method, hereby facilitating output from the method.

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

The DoAdd method adds three value parameters and passes the result back to the caller via an out parameter.

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


Read more about output parameters in the text book version of this material.