标签: null-coalescing-operator

PHP中的C#的空合并运算符(??)

在PHP中是否有像??C#一样的三元运算符?

?? 在C#中是干净和短,但在PHP中,你必须做的事情如下:

// This is absolutely okay except that $_REQUEST['test'] is kind of redundant.
echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi';

// This is perfect! Shorter and cleaner, but only in this situation.
echo null? : 'replacement if empty';

// This line gives error when $_REQUEST['test'] is NOT set.
echo $_REQUEST['test']?: 'hi';
Run Code Online (Sandbox Code Playgroud)

php ternary-operator null-coalescing-operator php-7

49
推荐指数
4
解决办法
1万
查看次数

?? Null Coalescing Operator - >合并是什么意思?

我很想撒谎并说英语是我的第二语言,但事实是我只是不知道'合并'是什么意思.我知道??C#中的"做什么",但这个名字对我来说没有意义.

我查了一下这个词,我理解它是'join'的同义词.'Null Join Operator'仍然没有意义.

有人可以开导我吗?

c# terminology coalesce null-coalescing-operator

44
推荐指数
4
解决办法
4472
查看次数

可以覆盖null合并运算符吗?

是否可以在C#中覆盖类的null-coalescing运算符?

比方说,如果实例为null,我想返回默认值,如果不是,则返回实例.代码看起来像这样:

   return instance ?? new MyClass("Default");  
Run Code Online (Sandbox Code Playgroud)

但是如果我想使用null-coalescing运算符来检查是否设置了MyClass.MyValue呢?

当然没有真正的需要(至少我是这么认为) - 所以在你回答"你为什么要这样做"之前 - 我只是好奇是否有可能.

.net c# operator-overloading null-coalescing-operator

41
推荐指数
3
解决办法
3850
查看次数

C# ??Ruby中的运算符?

有可能实现?? ?? Ruby中的运算符?

a = nil
b = 1

x = a ?? b # x should == 1
x = b ?? 2 # x should == 1
Run Code Online (Sandbox Code Playgroud)

ruby operators null-coalescing-operator

36
推荐指数
3
解决办法
7485
查看次数

是否有更优雅的方式来添加可空的整数?

我需要添加许多类型为nullable int的变量.我使用了空合并运算符将它降低到每行一个变量,但我感觉有更简洁的方法来做到这一点,例如我不能以某种方式将这些语句链接在一起,我已经在其他之前看到过码.

using System;

namespace TestNullInts
{
    class Program
    {
        static void Main(string[] args)
        {
            int? sum1 = 1;
            int? sum2 = null;
            int? sum3 = 3;

            //int total = sum1 + sum2 + sum3;
            //int total = sum1.Value + sum2.Value + sum3.Value;

            int total = 0;
            total = total + sum1 ?? total;
            total = total + sum2 ?? total;
            total = total + sum3 ?? total;

            Console.WriteLine(total);
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# nullable null-coalescing-operator

34
推荐指数
3
解决办法
2万
查看次数

是什么 "??" 运营商?

我想知道代码中的??标志C#.它是为了什么?我该如何使用它?

怎么样int??它是一个可以为空的int吗?

也可以看看:

?? Null Coalescing Operator - >合并是什么意思?

c# coalesce operators null-coalescing-operator

31
推荐指数
7
解决办法
2729
查看次数

是否可以使用操作员?并抛出新的异常()?

我接下来有很多方法:

var result = command.ExecuteScalar() as Int32?;
if(result.HasValue)
{
   return result.Value;
}
else
{
   throw new Exception(); // just an example, in my code I throw my own exception
}
Run Code Online (Sandbox Code Playgroud)

我希望我可以使用这样的运算符??:

return command.ExecuteScalar() as Int32? ?? throw new Exception();
Run Code Online (Sandbox Code Playgroud)

但它会生成编译错误.

是否可以重写我的代码,或者只有一种方法可以做到这一点?

.net c# nullable null-coalescing-operator

28
推荐指数
2
解决办法
8779
查看次数

奇怪的运算符优先级与?? (null合并运算符)

最近我有一个奇怪的错误,我在那里连接一个字符串int?然后添加另一个字符串.

我的代码基本上相当于:

int? x=10;
string s = "foo" + x ?? 0 + "bar";
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,这将运行和编译没有警告或不兼容的类型错误,这将是:

int? x=10;
string s = "foo" + x ?? "0" + "bar";
Run Code Online (Sandbox Code Playgroud)

然后这会导致意外的类型不兼容错误:

int? x=10;
string s = "foo" + x ?? 0 + 12;
Run Code Online (Sandbox Code Playgroud)

这个更简单的例子也是如此:

int? x=10;
string s = "foo" + x ?? 0;
Run Code Online (Sandbox Code Playgroud)

有人能解释一下这对我有用吗?

c# types null-coalescing-operator

22
推荐指数
2
解决办法
2502
查看次数

F#中的空融合运算符?

在与C#库交互时,我发现自己想要Nullable结构和引用类型的C#的空合并运算符.

是否有可能在F#中使用一个内联适当if情况的重载运算符对其进行近似?

null f# null-coalescing-operator null-coalescing

22
推荐指数
2
解决办法
3428
查看次数

Null合并运算符IList,Array,Enumerable.Empty in foreach

这个问题中,我发现了以下内容:

int[] array = null;

foreach (int i in array ?? Enumerable.Empty<int>())  
{  
    System.Console.WriteLine(string.Format("{0}", i));  
}  
Run Code Online (Sandbox Code Playgroud)

int[] returnArray = Do.Something() ?? new int[] {};
Run Code Online (Sandbox Code Playgroud)

... ?? new int[0]
Run Code Online (Sandbox Code Playgroud)

在一个NotifyCollectionChangedEventHandler我想申请Enumerable.Empty如此:

foreach (DrawingPoint drawingPoint in e.OldItems ?? Enumerable.Empty<DrawingPoint>())
    this.RemovePointMarker(drawingPoint);
Run Code Online (Sandbox Code Playgroud)

注意: OldItems 属于该类型 IList

它给了我:

接线员'??' 不能应用于'System.Collections.IList'类型的操作数System.Collections.Generic.IEnumerable<DrawingPoint>

然而

foreach (DrawingPoint drawingPoint in e.OldItems ?? new int[0])
Run Code Online (Sandbox Code Playgroud)

foreach (DrawingPoint drawingPoint in e.OldItems ?? new int[] {})
Run Code Online (Sandbox Code Playgroud)

工作得很好.

这是为什么?
为什么IList ?? T[]工作但IList …

.net c# collections null-coalescing-operator

22
推荐指数
1
解决办法
1095
查看次数