Javascript中是否有空合并运算符?
例如,在C#中,我可以这样做:
String someString = null;
var whatIWant = someString ?? "Cookies!";
Run Code Online (Sandbox Code Playgroud)
我可以为Javascript找出的最佳近似值是使用条件运算符:
var someString = null;
var whatIWant = someString ? someString : 'Cookies!';
Run Code Online (Sandbox Code Playgroud)
这有点ick嘿恕我直言.我可以做得更好吗?
javascript operators null-coalescing-operator null-coalescing
虽然我正在研究委托其是实际上是一个抽象类中Delegate.cs,我看到了下面的方法中,我不明白
?它已经是一个引用(类)类型?[]? 参数的含义你能解释一下吗?
public static Delegate? Combine(params Delegate?[]? delegates)
{
if (delegates == null || delegates.Length == 0)
return null;
Delegate? d = delegates[0];
for (int i = 1; i < delegates.Length; i++)
d = Combine(d, delegates[i]);
return d;
}
Run Code Online (Sandbox Code Playgroud) .net c# null-coalescing-operator null-coalescing nullable-reference-types
如何编写以下方案的简写?
get
{
if (_rows == null)
{
_rows = new List<Row>();
}
return _rows;
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
?? Null Coalescing Operator - >合并是什么意思?
C#中两个问号共同意味着什么?
我在这里找不到这个问题所以我想我会问它.双重问号在C#中有什么作用?
例:
x = y ?? z;
Run Code Online (Sandbox Code Playgroud) 在与C#库交互时,我发现自己想要Nullable结构和引用类型的C#的空合并运算符.
是否有可能在F#中使用一个内联适当if情况的重载运算符对其进行近似?
现在 C# 中是否有任何速记可以减少以下代码:
var testVar1 = checkObject();
if (testVar1 != null)
{
testVar2 = testVar1;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果从 CheckObject() 结果中 testVar1 不为空,则只想分配 testVar2(testVar2 有一个将触发代码的设置器)。试图思考如何使用空合并的东西,但没有真正解决。
添加到此 testVar2 的 setter 上有要触发的代码,因此如果值为 null,则不要将 testVar2 设置为任何内容。
public MyObj testVar2
{
get { return _testVar2; }
set
{
_testVar2 = value;
RunSomeCode();
}
}
Run Code Online (Sandbox Code Playgroud) 你认为C#会支持像?? = operator这样的东西吗?
而不是这个:
if (list == null)
list = new List<int>();
Run Code Online (Sandbox Code Playgroud)
有可能写:
list ??= new List<int>();
Run Code Online (Sandbox Code Playgroud)
现在,我可以使用(但在我看来,它不太可读):
list = list ?? new List<int>();
Run Code Online (Sandbox Code Playgroud) 所以,我知道,在 Python 中我可以这样做:
variable_name = other_variable or 'something else'
Run Code Online (Sandbox Code Playgroud)
...并且将分配'something else'给variable_nameifother_variable是假的,否则将other_variable值分配给variable。
我可以用字典做一个简洁的类似的事情吗:
variable_name = my_dict['keyname'] or 'something else'
Run Code Online (Sandbox Code Playgroud)
...或者不存在的键名总是会引发错误,导致失败?
我有属性类型int?并且decimal?正在计算中使用。如果其中任何一个的值为 null,则必须默认为 0。我试图在使用 null-coalescing 或GetValueOrDefault()如果值为 时也默认为 0 之间做出决定null。哪种方法在可读性和性能方面更好(如果有任何明显的差异)?
第一的:
public decimal MyMethod(int memberId)
{
var dto = GetDtoValue(memberId);
return (dto.PropertyOne ?? 0)
+ (dto.PropertyTwo ?? 0)
+ (dto.PropertyThree ?? 0)
- (dto.PropertyFour ?? 0)
+ ...
}
Run Code Online (Sandbox Code Playgroud)
第二:
public decimal MyMethod(int memberId)
{
var dto = GetDtoValue(memberId);
return dto.PropertyOne.GetValueOrDefault())
+ dto.PropertyTwo.GetValueOrDefault())
+ dto.PropertyThree.GetValueOrDefault())
- dto.PropertyFour.GetValueOrDefault())
+ ...
}
Run Code Online (Sandbox Code Playgroud) Python中是否有一个空传播运算符(“空感知成员访问”运算符),所以我可以写一些类似的东西
var = object?.children?.grandchildren?.property
Run Code Online (Sandbox Code Playgroud)
就像在 C#、VB.NET 和 TypeScript 中一样,而不是
var = None if not myobject\
or not myobject.children\
or not myobject.children.grandchildren\
else myobject.children.grandchildren.property
Run Code Online (Sandbox Code Playgroud) python null-coalescing-operator null-coalescing method-chaining
null-coalescing ×10
c# ×6
operators ×3
.net ×2
python ×2
dictionary ×1
f# ×1
javascript ×1
null ×1
shorthand ×1