Home  >  Article  >  Backend Development  >  The difference between ref and out in C#

The difference between ref and out in C#

WBOY
WBOYforward
2023-09-10 15:53:051001browse

C# 中 ref 和 out 的区别

In this article, we will understand the difference between “ref” and “out” in C#.

Ref Keyword

  • Before passing the parameter to 'ref', it needs to be initialized.

  • There is no need to initialize the parameter values ​​to the calling method before returning.

  • When using the 'ref' keyword, data can be passed in both directions.

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

Output Keyword

  • It is not necessary to initialize the parameters before passing them to "out".

  • The parameters need to be initialized before returning them to the caller.

  • When a method needs to return multiple values, it is helpful to declare parameters using the "out" keyword.

  • When using the "out" keyword, data is only passed in one direction.

The above is the detailed content of The difference between ref and out in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete