Difference Between ref and out in C#

CsharpServer Side ProgrammingProgramming

In this post, we will understand the difference between ‘ref’ and ‘out’ in C#.

Ref keyword

  • Before passing the parameters to ‘ref’, they need to be initialized.

  • It is not necessary to initialize the value of the parameter before it returns to the calling method.

  • The data can pass in two directions when the ‘ref’ keyword is used.

  • It is useful when the called method needs to change the value of the parameter that is passed.

Out keyword

  • It is not required to initialize parameters before it is passed to ‘out’.

  • It is required to initialize the value of a parameter before it is returned to the calling method.

  • Declaration of parameter using the ‘out’ keyword is helpful when a method needs to return multiple values.

  • When ‘out’ keyword is used, the data is passed in one direction only.

raja
Published on 24-Mar-2021 13:08:37
Advertisements