c# 4.0允许可选的out或ref参数吗?
当前回答
对于简单类型,您可以使用不安全的代码来实现这一点,尽管这不是惯用的,也不是推荐的。像这样:
// unsafe since remainder can point anywhere
// and we can do arbitrary pointer manipulation
public unsafe int Divide( int x, int y, int* remainder = null ) {
if( null != remainder ) *remainder = x % y;
return x / y;
}
也就是说,没有理论上的原因,c#最终不能使用安全的代码,比如下面的代码:
// safe because remainder must point to a valid int or to nothing
// and we cannot do arbitrary pointer manipulation
public int Divide( int x, int y, out? int remainder = null ) {
if( null != remainder ) *remainder = x % y;
return x / y;
}
事情可能会变得有趣:
// remainder is an optional output parameter
// (to a nullable reference type)
public int Divide( int x, int y, out? object? remainder = null ) {
if( null != remainder ) *remainder = 0 != y ? x % y : null;
return x / y;
}
其他回答
对于简单类型,您可以使用不安全的代码来实现这一点,尽管这不是惯用的,也不是推荐的。像这样:
// unsafe since remainder can point anywhere
// and we can do arbitrary pointer manipulation
public unsafe int Divide( int x, int y, int* remainder = null ) {
if( null != remainder ) *remainder = x % y;
return x / y;
}
也就是说,没有理论上的原因,c#最终不能使用安全的代码,比如下面的代码:
// safe because remainder must point to a valid int or to nothing
// and we cannot do arbitrary pointer manipulation
public int Divide( int x, int y, out? int remainder = null ) {
if( null != remainder ) *remainder = x % y;
return x / y;
}
事情可能会变得有趣:
// remainder is an optional output parameter
// (to a nullable reference type)
public int Divide( int x, int y, out? object? remainder = null ) {
if( null != remainder ) *remainder = 0 != y ? x % y : null;
return x / y;
}
No.
一个解决方法是重载另一个没有out / ref参数的方法,它只调用当前方法。
public bool SomeMethod(out string input)
{
...
}
// new overload
public bool SomeMethod()
{
string temp;
return SomeMethod(out temp);
}
如果你有c# 7.0,你可以简化:
// new overload
public bool SomeMethod()
{
return SomeMethod(out _); // declare out as an inline discard variable
}
(感谢@奥斯卡/ @赖纳指出这一点。)
正如已经提到的,这是不允许的,我认为这是非常有意义的。 然而,为了增加更多的细节,这里引用了c# 4.0规范21.1节:
Formal parameters of constructors, methods, indexers and delegate types can be declared optional: fixed-parameter: attributesopt parameter-modifieropt type identifier default-argumentopt default-argument: = expression A fixed-parameter with a default-argument is an optional parameter, whereas a fixed-parameter without a default-argument is a required parameter. A required parameter cannot appear after an optional parameter in a formal-parameter-list. A ref or out parameter cannot have a default-argument.
ICYMI:包括在这里列举的c# 7.0的新特性中,“discards”现在被允许以_的形式作为out参数,让你忽略你不关心的out参数:
p.GetCoordinates(out var x, out _);//我只关心x
附注:如果你也对“输出变量x”的部分感到困惑,请阅读链接上关于“输出变量”的新功能。
对于c# 6.0及以下版本,使用不带out形参的重载方法调用带out形参的方法。当被特别问到c# 4.0是否可以有一个可选的out参数时,我不确定为什么用于。net Core的c# 7.0甚至是这个线程的正确答案。答案是否定的!
推荐文章
- 新建T()
- 为什么空字典在Python中是一个危险的默认值?
- Maven命令行如何指向特定的settings.xml为单个命令?
- HttpListener访问被拒绝
- c# 4.0可选的out/ref参数
- c# 4.0中的“动态”类型是用来干什么的?
- 平行的。ForEach vs Task.Factory.StartNew
- 动态地向ExpandoObject添加属性
- x = x || y是什么意思?
- 在Tuple类中比"Item1", "Item2"更好的命名
- 普通参数与关键字参数
- .NET NewtonSoft JSON反序列化映射到不同的属性名
- 可选关键字参数的命名元组和默认值
- 如何定义具有可选参数的函数?
- 是否有一种方法在JavaScript函数调用中提供命名参数?