out
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out
You can use the out
contextual keyword in two contexts:
-
As a parameter modifier, which lets pass an argument to a method by reference rather than by value.
-
In generic type parameter declarations for interfaces and delegates, which specifies that a type parameter is covariant.
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.
http://www.yoda.arachsys.com/csharp/parameters.html
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);
|
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2015-07-06 Binary to Text (ASCII) Conversion
2015-07-06 Pizza pieces