Passing Parameters

Method parameters can be passed by value, by reference, or as out parameters. When a parameter is passed by value, the method gets a copy of the caller's data and cannot alter the caller's copy of the data. When a parameter is passed by reference, the method gets a pointer to the caller's data. This data is shared with the caller. If the method makes changes to a reference parameter, the changes are made to the caller's data. When using a reference parameter, the initial state of the data can be used by the method. An out parameter is similar to a reference parameter except that out parameters are used exclusively for returning data to the caller, while reference parameters can be used to pass data into the method as well as receive data from the method.

Avoid using out or reference parameters.

Working with members that define out or reference parameters requires that the developer understand pointers, subtle differences between value types and reference types, and initialization differences between out and reference parameters.

Do not pass reference types by reference.

Passing an object by reference allows the method to replace the object with a different instance. In most scenarios the method should use the supplied object and not be allowed to replace it. There are some limited exceptions to this rule (for example a method that can be used to swap references).

Portions Copyright 2005 Microsoft Corporation. All rights reserved.

Portions Copyright Addison-Wesley Corporation. All rights reserved.

For more information on design guidelines, see the "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" book by Krzysztof Cwalina and Brad Abrams, published by Addison-Wesley, 2005.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft