在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) 我很想撒谎并说英语是我的第二语言,但事实是我只是不知道'合并'是什么意思.我知道??C#中的"做什么",但这个名字对我来说没有意义.
我查了一下这个词,我理解它是'join'的同义词.'Null Join Operator'仍然没有意义.
有人可以开导我吗?
是否可以在C#中覆盖类的null-coalescing运算符?
比方说,如果实例为null,我想返回默认值,如果不是,则返回实例.代码看起来像这样:
return instance ?? new MyClass("Default");
Run Code Online (Sandbox Code Playgroud)
但是如果我想使用null-coalescing运算符来检查是否设置了MyClass.MyValue呢?
当然没有真正的需要(至少我是这么认为) - 所以在你回答"你为什么要这样做"之前 - 我只是好奇是否有可能.
有可能实现?? ?? Ruby中的运算符?
a = nil
b = 1
x = a ?? b # x should == 1
x = b ?? 2 # x should == 1
Run Code Online (Sandbox Code Playgroud) 我需要添加许多类型为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#.它是为了什么?我该如何使用它?
怎么样int??它是一个可以为空的int吗?
我接下来有很多方法:
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)
但它会生成编译错误.
是否可以重写我的代码,或者只有一种方法可以做到这一点?
最近我有一个奇怪的错误,我在那里连接一个字符串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#库交互时,我发现自己想要Nullable结构和引用类型的C#的空合并运算符.
是否有可能在F#中使用一个内联适当if情况的重载运算符对其进行近似?
在这个问题中,我发现了以下内容:
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 …