我正在尝试在int上使用null-coalescing运算符.当我在字符串上使用它时它可以工作
UserProfile.Name = dr["Name"].ToString()??"";
Run Code Online (Sandbox Code Playgroud)
当我尝试在这样的int上使用它时
UserProfile.BoardID = Convert.ToInt32(dr["BoardID"])??default(int);
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息
接线员'??' 不能应用于'int'和'int'类型的操作数
我找到了这篇博文,其中使用了http://davidhayden.com/blog/dave/archive/2006/07/05/NullCoalescingOperator.aspx和int数据类型.谁能说出我做错了什么?
我不知道:
但我认为意图非常明显.
public static class DebugLogic
{
public static bool ThrowIfNull = false;
public static T OrNew<T>(this T obj) where T : class
{
if (obj != null) return obj;
else if (ThrowIfNull) throw new ArgumentNullException(//to do...);
else return Activator.CreateInstance<T>();
}
}
Run Code Online (Sandbox Code Playgroud)
预期用途:
var customer = order.Sale.OrNew().Customer.OrNew().Name
我在做什么?这是疯了还是有帮助的?这似乎有帮助.
c# extension-methods null-coalescing-operator nullreferenceexception
我用 ??我的代码非常重视运算符.但今天我刚遇到一个问题.
这里我用的代码是什么?操作者
private List<string> _names;
public List<string> Names
{
get { return _names ?? (_names = new List<string>()); }
}
Run Code Online (Sandbox Code Playgroud)
但在某些地方我也看过这个代码.
private List<string> _names;
public List<string> Names
{
get { return _names ?? new List<string>(); }
}
Run Code Online (Sandbox Code Playgroud)
这些代码之间的真正区别是什么.在一个我分配_names = new List()而在其他我正在做新的List().
??如果左表达式为null或未定义,Spider的 null-coalescing运算符将返回正确的表达式.
var name = options.name ?? "default name";
Run Code Online (Sandbox Code Playgroud)
它是如何工作的?
在Swift中,如何设置可选项,foo但仅限于它nil?
foo = foo ?? "Hello"(在哪里foo可选String)
但是,有没有更好的方法,比如Ruby?
foo ||= "Hello"
在包含elvis运算符(即nullsafe解引用运算符;?.)的表达式上调用扩展方法时,结果null不会按预期传递给扩展方法.实质上,它可能会导致意外的空引用异常.这是一个展示这个的程序:
class Program
{
static void Main(string[] args)
{
string nil = null;
foreach (var c in ((nil?.ToCharArray()).EmptyIfDefault())) { }; // works
foreach (var c in (nil?.ToCharArray().EmptyIfDefault())) { }; // nullref
}
}
public static class Utility
{
public static char[] EmptyIfDefault(this char[] target)
{
return target ?? new char[0];
}
}
Run Code Online (Sandbox Code Playgroud)
如果这种行为是设计的吗?注意没有?ToCharArray()和EmptyIfDefault之间.如果有,我会理解当前的行为.现在,它似乎是一个错误.(将此报告给Microsoft的正确方法是什么?)
对于那些看到相同行为的人:额外的大括号似乎阻止了它.
(顺便说一句:这是我正在使用的实际EmptyIfNull :)
public static IEnumerable<TTarget> EmptyIfNull<TTarget>(this IEnumerable<TTarget> target)
{
return target ?? Enumerable.Empty<TTarget>();
}
Run Code Online (Sandbox Code Playgroud)
编辑我只是将下面给出的答案包含在我的问题中:
这与一个常见的陷阱有关:
var txt = "I am " +
age>=18 ? …Run Code Online (Sandbox Code Playgroud) 我正在看这个代码示例C# 7.0,我不确定引擎盖下发生了什么以及这个循环的性能.
foreach (var c in text ?? throw new ArgumentNullException(nameof(text)))
{
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
protected override void OnEnter(EventArgs e)
{
// this.Font = new Font(this.Font, FontStyle.Italic);
base.BackColor = _colors.SelectedBackColor ?? base.BackColor;
base.ForeColor = _colors.SelectedForeColor ?? base.BackColor;
base.OnEnter(e);
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
错误519运算符'??' 不能应用于'System.Drawing.Color'和'System.Drawing.Color'类型的操作数
我认为它必须是2匹配类型的空合并
它工作正常,不会造成问题
echo $formErrorBag[ 'email' ] ?? null
Run Code Online (Sandbox Code Playgroud)
但这是一种公认的做法吗?从未见过这样的例子null.
null合并运算符(??)返回其第一个操作数(如果它存在且不为NULL),否则返回其第二个操作数.
如果第一个操作数是函数或方法调用,操作符是否会调用函数调用两次?
例如,假设该函数get_name()返回一个字符串值或null.
$name = get_name() ?? 'no name found';
因此,被get_name()调用一次,值存储准备把它赋值给变量($name),或者当??被激活引起的函数返回一个值,是真正的isset(),并??在第一个操作数调用第二次获得的价值?
c# ×6
php ×2
.net ×1
c#-7.0 ×1
dereference ×1
javascript ×1
null ×1
operators ×1
php-7 ×1
spiderlang ×1
swift ×1