Tag: null coalescing operator

理解空合并运算符(??)

我有一个自定义WebControl ,它实现了一个返回Nullable 的.Value getter / setter 它是一个客户端过滤的文本框( TextBox的子类,包含javascript和一些用于设置/获取值的服务器端逻辑) 这是来自该控件的getter和setter: public decimal? Value { get { decimal amount = 0; if (!decimal.TryParse(this.Text, NumberStyles.Currency, null, out amount)) { return null; } else { return amount; } } set { if (!value.HasValue) { this.Text = “”; } else { this.Text = string.Format(“${0:#,##0.00}”, value); } } } 我看到的问题是这个语句的输出: decimal […]

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

我接下来有很多方法: 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 } 我希望我可以使用运算符?? 像这样: return command.ExecuteScalar() as Int32? ?? throw new Exception(); 但它会生成编译错误。 是否可以重写我的代码,或者只有一种方法可以做到这一点?

为什么空合并运算符(??)在这种情况下不起作用?

当我运行此代码时,我得到一个意外的NullReferenceException ,省略了fileSystemHelper参数(因此将其默认为null): public class GitLog { FileSystemHelper fileSystem; /// /// Initializes a new instance of the class. /// /// The path to a Git working copy. /// A helper class that provides file system services (optional). /// Thrown if the path is invalid. /// Thrown if there is no Git repository at the specified path. […]

Null-coalescing运算符和lambda表达式

看看我试图在构造函数中编写的以下代码: private Predicate _isValid; //… Predicate isValid = //…; this._isValid = isValid ?? s => true; 代码无法编译 – 只是“无效的表达式术语”等等。 相比之下,编译,我可以使用它: this._isValid = isValid ?? new Predicate(s => true); 但是,我仍然想知道为什么不允许这种语法。 有任何想法吗?

C#null coalescing运算符等效于c ++

C#null合并运算符是否有C ++等价物? 我在代码中进行了太多的空检查。 所以正在寻找一种减少空代码量的方法。

是否可以在C#中合并字符串和DBNull?

我正在编写一个C#例程来调用存储过程。 在我传入的参数列表中,其中一个值可能合法地为空。 所以我以为我会使用这样的一行: cmd.Parameters.Add(new SqlParameter(“@theParam”, theParam ?? DBNull.Value)); 不幸的是,这会返回以下错误: CS0019:运营商’??’ 不能应用于’string’和’System.DBNull’类型的操作数 现在,这似乎已经足够清楚了,但我不明白其背后的基本原理。 为什么这不起作用? (通常情况下,当我不明白为什么某些东西不起作用时,并不是它不起作用……而是我做错了。) 我是否真的需要将其延伸到更长的if-then声明中? 编辑:(顺便说一句,对那些建议只使用“null”的人来说,它不起作用。我原来认为null也会自动翻译成DBNull,但它显然没有。(谁知道?))

Null-coalescing运算符为动态对象的属性返回null

我最近在使用Json.NET将JSON解析为动态对象时发现了null-coalescing运算符的问题。 假设这是我的动态对象: string json = “{ \”phones\”: { \”personal\”: null }, \”birthday\”: null }”; dynamic d = JsonConvert.DeserializeObject(json); 如果我尝试使用?? 运算符在d的一个字段上,它返回null: string s = “”; s += (d.phones.personal ?? “default”); Console.WriteLine(s + ” ” + s.Length); //outputs 0 但是,如果我将动态属性分配给字符串,那么它可以正常工作: string ss = d.phones.personal; string s = “”; s += (ss ?? “default”); Console.WriteLine(s + ” ” […]

隐式转换Null-Coalescing运算符结果

通过以下对C#中的空合并运算符(??)的理解。 int? input = -10; int result = input ?? 10;//Case – I //is same as: int result = input == null? input : 10; // Case – II 而根据定义和用法,案例I和案例II是相同的。 令人惊讶的是,在Case-I编译器能够隐式转换int? to case而在Case-II中它显示错误:’错误1无法隐式转换类型’int?’ ‘int’“ 关于null-coalescing运算符,我错过了什么? 谢谢你的关注。

可以使用?? (合并运算符)与DBNull?

如果我有类似以下的代码: while(myDataReader.Read()) { myObject.intVal = Convert.ToInt32(myDataReader[“mycolumn”] ?? 0); } 它抛出错误: 无法将对象从DBNull强制转换为其他类型。 将intVal定义为可为空的int不是一个选项。 有没有办法让我做到以上几点?

C#null-coalescing(??)运算符的运算符优先级是什么?

我刚试过以下内容,想法是连接两个字符串,用空字符串替换空值。 string a=”Hello”; string b=” World”; – 调试(有趣的是?是打印,并不完全有助于提高可读性……) ? a ?? “” + b ?? “” – >“你好” 正确的是: ? (a??””)+(b??””) “Hello World” 如果a为null,我有点期待“Hello World”,或者只是“World”。 显然,这是运算符优先级的todo,可以通过括号来克服,是否存在记录此新运算符的优先顺序的任何位置。 (意识到我应该使用stringbuilder或String.Concat) 谢谢。