带有 ref 关键字的空合并运算符
Null-coalescing operator with ref keyword
编辑
这是对我的问题的更好解释。特别是回答问题 "Why I want to use references, and why not just use double?
".
所以我实际上有以下变量:
double? Left_A, Left_B;
double? Right_A, Right_B;
double? Result_Left, Result_Right;
用户要么设置左侧变量的值,要么设置右侧变量的值。它在我的 ViewModel 中处理。我正在计算结果的值,基于
一些公式,如 result = a * b
等
左右变量的公式相同。
所以我只想创建一个 "pointer" 类的参考变量,'a' 和 'b',其值将是 Left_A
或 [=17= 的值] 这样我就不必执行以下操作:
if(Left_A != null) {
Result_Left = Left_A * Left_B;
} else {
Result_Right = Right_A * Right_B;
}
//There are more such formulas for many use-cases..
我想要这样的东西..
ref double? result, a, b;
a = ref Left_A ?? ref Right_A; //This gives error.
b = ref (Left_B ?? Right_B); //This gives error.
result = Result_Left ?? Result_Right;
result = a * b;
希望我没有做错什么..
我正在尝试使用 Null-coalescing operator with the ref
keyword
我的赋值语句如下:
注意:根据我这里省略的业务逻辑,保证a&b都不会为null。它们中的任何一个都会有一个值。
double? x = a ?? b; // a and b are also of type "double?".
不过我希望x
是一个引用类型变量。这可能吗?
我尝试了以下方法,但都出现编译错误。特别是最后一个:
ref double? x = ref (a ?? b);
ref double? x = ref a ?? ref b;
ref double? x = (ref a) ?? (ref b);
ref double? param1 = ref ( please work!!! -.-' );
有什么想法吗?
//It is guaranteed that either a/b is not null.
不能保证。它只是检查 a
是否为空,如果是,则将 b
分配给 x
.
如果你的两个变量都是 double?
并且你不需要结果可以为空,那么你可以使用 .GetValueOrDefault()
:
double x = (a ?? b).GetValueOrDefault();
double x = (a ?? b).GetValueOrDefault(-0.5); // Or this.
可以用一个方法来完成:
ref double? GetValue(ref double? a, ref double? b) {
if (a == null) return ref b; else return ref a;
}
那么,
ref double? x = ref GetValue(ref a, ref b);
我认为使用空合并运算符无法完成。
这里不需要使用ref
关键字。
以下将起作用:
double a = (double) (Left_A ?? Right_A);
double b = (double) (Left_B ?? Right_B);
double result = a * b;
或使用一根衬垫:
result = (double) (leftA ?? rightA) * (double) (leftB ?? rightB);
编辑
这是对我的问题的更好解释。特别是回答问题 "Why I want to use references, and why not just use double?
".
所以我实际上有以下变量:
double? Left_A, Left_B;
double? Right_A, Right_B;
double? Result_Left, Result_Right;
用户要么设置左侧变量的值,要么设置右侧变量的值。它在我的 ViewModel 中处理。我正在计算结果的值,基于
一些公式,如 result = a * b
等
左右变量的公式相同。
所以我只想创建一个 "pointer" 类的参考变量,'a' 和 'b',其值将是 Left_A
或 [=17= 的值] 这样我就不必执行以下操作:
if(Left_A != null) {
Result_Left = Left_A * Left_B;
} else {
Result_Right = Right_A * Right_B;
}
//There are more such formulas for many use-cases..
我想要这样的东西..
ref double? result, a, b;
a = ref Left_A ?? ref Right_A; //This gives error.
b = ref (Left_B ?? Right_B); //This gives error.
result = Result_Left ?? Result_Right;
result = a * b;
希望我没有做错什么..
我正在尝试使用 Null-coalescing operator with the ref
keyword
我的赋值语句如下:
注意:根据我这里省略的业务逻辑,保证a&b都不会为null。它们中的任何一个都会有一个值。
double? x = a ?? b; // a and b are also of type "double?".
不过我希望x
是一个引用类型变量。这可能吗?
我尝试了以下方法,但都出现编译错误。特别是最后一个:
ref double? x = ref (a ?? b);
ref double? x = ref a ?? ref b;
ref double? x = (ref a) ?? (ref b);
ref double? param1 = ref ( please work!!! -.-' );
有什么想法吗?
//It is guaranteed that either a/b is not null.
不能保证。它只是检查 a
是否为空,如果是,则将 b
分配给 x
.
如果你的两个变量都是 double?
并且你不需要结果可以为空,那么你可以使用 .GetValueOrDefault()
:
double x = (a ?? b).GetValueOrDefault();
double x = (a ?? b).GetValueOrDefault(-0.5); // Or this.
可以用一个方法来完成:
ref double? GetValue(ref double? a, ref double? b) {
if (a == null) return ref b; else return ref a;
}
那么,
ref double? x = ref GetValue(ref a, ref b);
我认为使用空合并运算符无法完成。
这里不需要使用ref
关键字。
以下将起作用:
double a = (double) (Left_A ?? Right_A);
double b = (double) (Left_B ?? Right_B);
double result = a * b;
或使用一根衬垫:
result = (double) (leftA ?? rightA) * (double) (leftB ?? rightB);