out

发布时间:2017-7-9 7:09:46编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"out ",主要涉及到out 方面的内容,对于out 感兴趣的同学可以参考一下。

out parameter modifier (C# Reference)

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier

When should I use out parameters?

Out is good when you have a TryNNN function and it's clear that the out-parameter will always be set even if the function does not succeed.

This allows you rely on the fact that the local variable you declare will be set rather than having to place checks later in your code against null.

(A comment below indicates that the parameter could be set to null, so you may want to verify the documentation for the function you're calling to be sure if this is the case or not.)

It makes the code a little clearer and easier to read.

Another case is when you need to return some data and a status on the condition of the method like:

public bool DoSomething(int arg1, out string result);

In this case the return can indicate if the function succeeded and the result is stored in the out parameter.

Admittedly, this example is contrived because you can design a way where the function simply returns a string, but you get the idea.

A disadvantage is that you have to declare a local variable to use them:

string result;
if (DoSomething(5, out result))
    UpdateWithResult(result);

Instead of:

UpdateWithResult(DoSomething(5));

However, that may not even be a disadvantage, it depends on the design you're going for.

In the case of DateTime, both means (Parse and TryParse) are provided.


上一篇:Java学习笔记—多态
下一篇:Diagnostics: File file:/tmp/spark-***/__spark_libs__***.zip does not exist

相关文章

关键词: out

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

最近更新

好贷网好贷款