且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是在C#中使用out参数

更新时间:2022-10-15 20:13:51

良好的使用退出参数的***的例子是在的TryParse 的方法。

  INT结果= -1;
如果(!Int32.TryParse(SomeString,出结果){
    //登录错误的输入
}

返回结果;
 

使用的TryParse 而不是 parseInt函数消除了需要处理异常,使code更优雅

退出参数基本上是允许从一个方法不止一个返回值。

Can you please tell me what is the exact use of out parameter?

Related Question:
What is the difference between ref and out? (C#)

The best example of a good use of an out parameter are in the TryParse methods.

int result =-1;
if (!Int32.TryParse(SomeString, out result){
    // log bad input
}

return result;

Using TryParse instead of ParseInt removes the need to handle exceptions and makes the code much more elegant.

The out parameter essentially allows for more than one return values from a method.