null-conditional-operator

Using null-conditional bool? in if statement [duplicate]

守給你的承諾、 提交于 2021-01-22 03:40:06
问题 This question already has answers here : Converting Nullable<bool> to bool (6 answers) Compiler Error for Nullable Bool (5 answers) cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) (9 answers) Closed 3 years ago . Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making such an implicit conversion

Using null-conditional bool? in if statement [duplicate]

你说的曾经没有我的故事 提交于 2021-01-22 03:36:32
问题 This question already has answers here : Converting Nullable<bool> to bool (6 answers) Compiler Error for Nullable Bool (5 answers) cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) (9 answers) Closed 3 years ago . Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making such an implicit conversion

Using null-conditional bool? in if statement [duplicate]

ぃ、小莉子 提交于 2021-01-22 03:36:29
问题 This question already has answers here : Converting Nullable<bool> to bool (6 answers) Compiler Error for Nullable Bool (5 answers) cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) (9 answers) Closed 3 years ago . Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making such an implicit conversion

Using null-conditional bool? in if statement [duplicate]

前提是你 提交于 2021-01-22 03:36:27
问题 This question already has answers here : Converting Nullable<bool> to bool (6 answers) Compiler Error for Nullable Bool (5 answers) cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) (9 answers) Closed 3 years ago . Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making such an implicit conversion

Using null-conditional bool? in if statement [duplicate]

落花浮王杯 提交于 2021-01-22 03:34:24
问题 This question already has answers here : Converting Nullable<bool> to bool (6 answers) Compiler Error for Nullable Bool (5 answers) cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) (9 answers) Closed 3 years ago . Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making such an implicit conversion

What is meant by “the null conditional operator short circuits”?

只谈情不闲聊 提交于 2019-12-31 05:15:31
问题 Note to future visitors: This question was based on faulty repro code. The ?. operator does indeed short circuit. You can close this browser tab now. There are many sources on the web that claim that the null conditional operator ( ?. ) short circuits (e.g. http://www.informit.com/articles/article.aspx?p=2421572, search for "circuit"). I cannot detect any such thing: static void Main() { var c = new C(); Console.WriteLine(c?.Inner?.Inner); //does not rely on short circuiting, works Console

Why can I omit the subsequent null-conditional operators in an invocation chain?

瘦欲@ 提交于 2019-12-19 11:10:59
问题 Consider the following code: IEnumerable<int> xx = null; var tt = xx?.Where(x => x > 2).Select(x => x.ToString()); It assigns null to tt . The question is: why does it work properly? I thought I must use ?. before Select as ?.Where(...) returns null . Besides, if I split the second line into two separate lines: IEnumerable<int> xx = null; var yy = xx?.Where(x => x > 2); var zz = yy.Select(x => x.ToString()); There will be the ArgumentNullException on the third line as yy == null . What's the

Why can I omit the subsequent null-conditional operators in an invocation chain?

旧街凉风 提交于 2019-12-19 11:10:48
问题 Consider the following code: IEnumerable<int> xx = null; var tt = xx?.Where(x => x > 2).Select(x => x.ToString()); It assigns null to tt . The question is: why does it work properly? I thought I must use ?. before Select as ?.Where(...) returns null . Besides, if I split the second line into two separate lines: IEnumerable<int> xx = null; var yy = xx?.Where(x => x > 2); var zz = yy.Select(x => x.ToString()); There will be the ArgumentNullException on the third line as yy == null . What's the

Using the Null Conditional Operator to check values on objects which might be null

时间秒杀一切 提交于 2019-12-08 14:41:39
问题 I've been playing with C# 6's Null Conditional Operator (more info here). I really like the syntax and I think it makes the code much more readable however I think it is questionable as to what exactly the code is going to do when you come across checking the value of a property on an object which itself might be null. For example, if I had a class with a decimal property on it and I wanted a conditional check on the value of that decimal, I would write something like: if (foo?.Bar > max) { /

C# Error with null-conditional operator and await

时间秒杀一切 提交于 2019-12-06 19:04:53
问题 I'm experiencing an interesting System.NullReferenceException whilst using the new null-conditional operator in C#. The following code gives me a NullReferenceException if "MyObject" is null: await this.MyObject?.MyMethod() I would've expected that the call to "MyMethod" would simply not be made if "MyObject" is null, or am I misunderstanding the purpose of the null-conditional operator? 回答1: You can add ?? Operator so if ?. returns null task use CompletedTask instead. await (this.MyObject?