Lecture overview -- Keyboard shortcut: 'u'  Previous page: Passing structs as value parameters -- Keyboard shortcut: 'p'  Next page: Output Parameters -- 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 24 : 29
Object-oriented Programming in C#
Data Access, Properties, and Methods
Reference Parameters

Here we discuss ref parameters ala C#. This is analogous to var (variable parameters) known from Pascal.

Reference parameters can be used for both input to and output from methods

  • Reference parameter passing

    • The corresponding argument must be a variable, and it must have a value

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

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

    • The actual parameter must be prefixed with the keyword ref

A formal ref parameter serve as an alias - an alternative name - of the corresponding actual parameter.

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

A classical use of a reference parameter. The Swap method exchanges the values of two variables.

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

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