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

Be warned! This is not a discussion of C# reference parameters. It is a discussion of how to pass references via value parameters.

Care must be taken if we pass references as value parameters

/user/normark/oop-csharp-1/sources/c-sharp/method-parameters/by-value/class/date.csThe class Date and the method DayDifference with a value parameter. This program is explained

This class introduces the DayDifference method, which accepts a formal Date parameter.

/user/normark/oop-csharp-1/sources/c-sharp/method-parameters/by-value/class/person.csThe class Person which calls DayDifference on dateOfBirth. This program is explained

We pass a reference to a Date object as an actual parameter.

/user/normark/oop-csharp-1/sources/c-sharp/method-parameters/by-value/class/Client.csA client of Person and Date which reveal the consequences. This program is explained

A class with a Main method which - indirectly - calls the method of interest.

/user/normark/oop-csharp-1/sources/c-sharp/method-parameters/by-value/class/outputClient program output.

The output that appears if you run the program shown above.

Go to exercisePassing references as ref parameters

In case a reference is passed as an argument to a value parameter, the referenced object can be modified through the formal parameter

This is the main point to remember from this page!

Read more about references passed by value in the text book version of this material.