前几天有人问我他们什么时候应该使用参数关键字out而不是ref。虽然我(我认为)理解ref和out关键字之间的区别(之前已经问过了),最好的解释似乎是ref == in和out,有什么(假设或代码)的例子,我应该总是使用out而不是ref。
既然ref更通用,为什么还要用out呢?它只是语法上的糖吗?
前几天有人问我他们什么时候应该使用参数关键字out而不是ref。虽然我(我认为)理解ref和out关键字之间的区别(之前已经问过了),最好的解释似乎是ref == in和out,有什么(假设或代码)的例子,我应该总是使用out而不是ref。
既然ref更通用,为什么还要用out呢?它只是语法上的糖吗?
当前回答
关于c# 7的额外注意事项: 在c# 7中,不需要使用out预先声明变量。代码是这样的:
public void PrintCoordinates(Point p)
{
int x, y; // have to "predeclare"
p.GetCoordinates(out x, out y);
WriteLine($"({x}, {y})");
}
可以这样写:
public void PrintCoordinates(Point p)
{
p.GetCoordinates(out int x, out int y);
WriteLine($"({x}, {y})");
}
来源:c# 7的新特性。
其他回答
基本上ref和out都用于在方法之间传递对象/值
out关键字使参数通过引用传递。这类似于ref关键字,只不过ref要求在传递变量之前对其进行初始化。
out:参数没有初始化,必须在方法中初始化
ref:参数已经初始化,可以在方法中读取和更新。
引用类型的“ref”有什么用?
您可以将给定的引用更改为不同的实例。
你知道吗?
Although the ref and out keywords cause different run-time behavior, they are not considered part of the method signature at compile time. Therefore, methods cannot be overloaded if the only difference is that one method takes a ref argument and the other takes an out argument. You can't use the ref and out keywords for the following kinds of methods: Async methods, which you define by using the async modifier. Iterator methods, which include a yield return or yield break statement. Properties are not variables and therefore cannot be passed as out parameters.
在c#中如何使用in或out或ref ?
All keywords in C# have the same functionality but with some boundaries. in arguments cannot be modified by the called method. ref arguments may be modified. ref must be initialized before being used by caller it can be read and updated in the method. out arguments must be modified by the caller. out arguments must be initialized in the method Variables passed as in arguments must be initialized before being passed in a method call. However, the called method may not assign a value or modify the argument.
以下类型的方法不能使用in、ref和out关键字:
Async方法,通过使用Async修饰符来定义。 迭代器方法,其中包括yield return或yield break语句。
如果计划读取和写入参数,则需要使用ref。如果你只打算写,你需要使用out。实际上,当您需要多个返回值时,或者当您不想使用正常的返回机制进行输出时(但这种情况应该很少发生),就会使用out。
有一些语言机制可以帮助这些用例。Ref形参在传递给方法之前必须已经初始化(强调它们是可读可写的),out形参在被赋值之前不能被读取,并且保证在方法结束时已经被写入(强调它们只能被写入)。违反这些原则将导致编译时错误。
int x;
Foo(ref x); // error: x is uninitialized
void Bar(out int x) {} // error: x was not written to
例如,int。TryParse返回bool类型,并接受out int形参:
int value;
if (int.TryParse(numericString, out value))
{
/* numericString was parsed into value, now do stuff */
}
else
{
/* numericString couldn't be parsed */
}
这是需要输出两个值的一个明确示例:数值结果和转换是否成功。CLR的作者决定在这里选择out,因为他们不关心int之前可能是什么。
作为参考,你可以看联锁。增量:
int x = 4;
Interlocked.Increment(ref x);
联锁。Increment自动地增加x的值。因为需要读取x来增加它,所以在这种情况下ref更合适。你完全关心x在传递给Increment之前是什么。
在c#的下一个版本中,甚至可以声明变量In out形参,更加强调它们只输出的性质:
if (int.TryParse(numericString, out int value))
{
// 'value' exists and was declared in the `if` statement
}
else
{
// conversion didn't work, 'value' doesn't exist here
}
作为ref传递的参数必须在传递给方法之前初始化,而out形参在传递给方法之前不需要初始化。
使用out表示该参数未被使用,仅被设置。这有助于调用者理解您总是在初始化参数。
此外,ref和out不仅适用于值类型。它们还允许您重置引用类型从方法中引用的对象。