Passing Parameters in C#

In C#, parameters can be passed either by value or by reference. Passing parameters by reference allows function members (methods, properties, indexers, operators, and constructors) to change the value of the parameters and have that change persist. To pass a parameter by reference, use the ref or out keyword. For simplicity, only the ref keyword is used in the examples of this topic. For information on the difference between ref and out, see ref, out, and Passing Arrays Using ref and out.

This topic includes the following sections:

It also includes the following examples:

Example Demonstrates Uses ref or out
1 Passing value types by value No
2 Passing value types by reference Yes
3 Swapping value types (two integers) Yes
4 Passing reference types by value No
5 Passing reference types by reference Yes
6 Swapping reference types (two strings) Yes


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s