Difference Between ref and out in C#

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 parameters using the ‘out’ keyword is helpful when a method needs to return multiple values.

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

0 Comment's

Comment Form