null-coalescing-operator

What is the proper way to check for null values?

心不动则不痛 提交于 2019-11-27 17:05:39
I love the null-coalescing operator because it makes it easy to assign a default value for nullable types. int y = x ?? -1; That's great, except if I need to do something simple with x . For instance, if I want to check Session , then I usually end up having to write something more verbose. I wish I could do this: string y = Session["key"].ToString() ?? "none"; But you can't because the .ToString() gets called before the null check so it fails if Session["key"] is null. I end up doing this: string y = Session["key"] == null ? "none" : Session["key"].ToString(); It works and is better, in my

Null coalescing in powershell

丶灬走出姿态 提交于 2019-11-27 11:24:19
Is there a null coalescing operator in powershell? I'd like to be able to do these c# commands in powershell: var s = myval ?? "new value"; var x = myval == null ? "" : otherval; No need for the Powershell Community Extensions, you can use the standard Powershell if statements as an expression: variable = if (condition) { expr1 } else { expr2 } So to the replacements for your first C# expression of: var s = myval ?? "new value"; becomes one of the following (depending on preference): $s = if ($myval -eq $null) { "new value" } else { $myval } $s = if ($myval -ne $null) { $myval } else { "new

?? Null Coalescing Operator --> What does coalescing mean?

故事扮演 提交于 2019-11-27 09:11:05
I'm tempted to lie and say that English is my second language, but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me. I looked up the word and I understand it to be a synonym for 'join'. 'Null Join Operator' still doesn't make sense. Can someone enlighten me? I'm tempted to lie and say that English is my second language...but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me. I looked up the word and I understand it to be a synonym for

Weird operator precedence with ?? (null coalescing operator)

只谈情不闲聊 提交于 2019-11-27 09:01:55
Recently I had a weird bug where I was concatenating a string with an int? and then adding another string after that. My code was basically the equivalent of this: int? x=10; string s = "foo" + x ?? 0 + "bar"; Amazingly enough this will run and compile without warnings or incompatible type errors, as will this: int? x=10; string s = "foo" + x ?? "0" + "bar"; And then this results in an unexpected type incompatibility error: int? x=10; string s = "foo" + x ?? 0 + 12; As will this simpler example: int? x=10; string s = "foo" + x ?? 0; Can someone explain how this works to me? Mark Byers The null

Implicit casting of Null-Coalescing operator result

孤人 提交于 2019-11-27 07:48:08
问题 With the following understanding about null coalescing operator (??) in C#. int? input = -10; int result = input ?? 10;//Case - I //is same as: int result = input == null? input : 10; // Case - II While, by definition and usage, Case I and Case II are same. It is surprising to see that in Case-I compiler is able to implicitly cast int? to int while in Case-II it shows error: 'Error 1 Cannot implicitly convert type 'int?' to 'int'" What is it that I am missing about null-coalescing operator?

Coalesce operator and Conditional operator in VB.NET [duplicate]

纵然是瞬间 提交于 2019-11-27 06:37:43
问题 Possible Duplicate: Is there a conditional ternary operator in VB.NET? Hi guys, Can we use Coalesce operator(??) and conditional ternary operator(:) in VB.NET as in C#? 回答1: I think you can get close with using an inline if statement: //C# int x = a ? b : c; 'VB.Net Dim x as Integer = If(a, b, c) 回答2: Sub Main() Dim x, z As Object Dim y As Nullable(Of Integer) z = "1243" Dim c As Object = Coalesce(x, y, z) End Sub Private Function Coalesce(ByVal ParamArray x As Object()) Return x.First

Null-coalescing operator and lambda expression

怎甘沉沦 提交于 2019-11-27 06:11:08
问题 take a look at the following code I attempted to write inside a constructor: private Predicate<string> _isValid; //... Predicate<string> isValid = //...; this._isValid = isValid ?? s => true; The code doesn't compile - just "invalid expression term"s and so one. In contrast that does compile and I could just use it: this._isValid = isValid ?? new Predicate<string>(s => true); However, I still wonder why this syntax is not allowed. Any ideas? 回答1: this._isValid = isValid ?? (s => true); Will

Curious null-coalescing operator custom implicit conversion behaviour

自闭症网瘾萝莉.ら 提交于 2019-11-27 05:45:44
Note: this appears to have been fixed in Roslyn This question arose when writing my answer to this one , which talks about the associativity of the null-coalescing operator . Just as a reminder, the idea of the null-coalescing operator is that an expression of the form x ?? y first evaluates x , then: If the value of x is null, y is evaluated and that is the end result of the expression If the value of x is non-null, y is not evaluated, and the value of x is the end result of the expression, after a conversion to the compile-time type of y if necessary Now usually there's no need for a

an expression tree lambda may not contain a null propagating operator

血红的双手。 提交于 2019-11-27 04:56:55
Question : The line price = co?.price ?? 0, in the following code gives me the above error. but if I remove ? from co.? it works fine. I was trying to follow this MSDN example where they are using ? on line select new { person.FirstName, PetName = subpet?.Name ?? String.Empty }; So, it seems I need to understand when to use ? with ?? and when not to. Error : an expression tree lambda may not contain a null propagating operator public class CustomerOrdersModelView { public string CustomerID { get; set; } public int FY { get; set; } public float? price { get; set; } .... .... } public async Task

What is the proper way to check for null values?

纵然是瞬间 提交于 2019-11-27 04:11:08
问题 I love the null-coalescing operator because it makes it easy to assign a default value for nullable types. int y = x ?? -1; That's great, except if I need to do something simple with x . For instance, if I want to check Session , then I usually end up having to write something more verbose. I wish I could do this: string y = Session["key"].ToString() ?? "none"; But you can't because the .ToString() gets called before the null check so it fails if Session["key"] is null. I end up doing this: