博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
out
阅读量:5031 次
发布时间:2019-06-12

本文共 3153 字,大约阅读时间需要 10 分钟。

You can use the out contextual keyword in two contexts:

  • As a , which lets pass an argument to a method by reference rather than by value.

  • In for interfaces and delegates, which specifies that a type parameter is covariant.

 

out parameter modifier (C# Reference)

 

 

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.

 

 

 

Output parameters

Like reference parameters, output parameters don't create a new storage location, but use the storage location of the variable specified on the invocation. Output parameters need the out modifier as part of both the declaration and the invocation - that means it's always clear when you're passing something as an output parameter.

Output parameters are very similar to reference parameters. The only differences are:

  • The variable specified on the invocation doesn't need to have been assigned a value before it is passed to the function member. If the function member completes normally, the variable is considered to be assigned afterwards (so you can then "read" it).
  • The parameter is considered initially unassigned (in other words, you must assign it a value before you can "read" it in the function member).
  • The parameter must be assigned a value before the function member completes normally.

Here is some example code showing this, with an int parameter (int is a value type, but if you understood reference parameters properly, you should be able to see what the behaviour for reference types is):

void Foo (out int x){    // Can't read x here - it's considered unassigned    // Assignment - this must happen before the method can complete normally    x = 10;    // The value of x can now be read: int a = x; } ... // Declare a variable but don't assign a value to it int y; // Pass it in as an output parameter, even though its value is unassigned Foo (out y); // It's now assigned a value, so we can write it out: Console.WriteLine (y);

 

 

转载于:https://www.cnblogs.com/chucklu/p/7125355.html

你可能感兴趣的文章
WEB测试和APP测试区别
查看>>
[物理学与PDEs]第4章习题4 一维理想反应流体力学方程组的守恒律形式及其 R.H. 条件...
查看>>
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.3
查看>>
[数分提高]2014-2015-2第4教学周第2次课
查看>>
ansible进阶小技巧--tags
查看>>
JSP页面跳转方式
查看>>
发布高性能迷你React框架anu
查看>>
Python中Gradient Boosting Machine(GBM)调参方法详解
查看>>
利用DDE通信将PLC数据传输到EXCEL
查看>>
Eclipse 实用快捷键大全
查看>>
与非门和或门实现异或门
查看>>
golang统计出其中英文字母、空格、数字和其它字符的个数
查看>>
poj 1782 Run Length Encoding
查看>>
《自我介绍》
查看>>
在线考试系统设计思路
查看>>
p1150[noip2013普及]表达式求值
查看>>
POST和GET有什么区别?
查看>>
js基础
查看>>
mmsplayer for ios v1.0
查看>>
Python ghost.py 0.2版简单实例
查看>>