null-coalescing-operator

?? Coalesce for empty string?

社会主义新天地 提交于 2019-11-30 06:14:41
问题 Something I find myself doing more and more is checking a string for empty (as in "" or null) and a conditional operator. A current example: s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber; This is just an extension method, it's equivalent to: string.IsNullOrEmpty(s.SiteNumber) ? "No Number" : s.SiteNumber; Since it's empty and not null, ?? won't do the trick. A string.IsNullOrEmpty() version of ?? would be the perfect solution. I'm thinking there has to be a cleaner way of doing

C# Reflection get Field or Property by Name

∥☆過路亽.° 提交于 2019-11-29 13:43:28
Is there a way to supply a name to a function that then returns the value of either the field or property on a given object with that name? I tried to work around it with the null-coalesce operator, but apparently that doesn't like different types (which is also a bit weird to me because null is null). I could separate it it out into if nulls, but there has to be a better way to do this. Here is my function, and the two lines with Comparison objects don't compile, but I will leave them in there to show what I am trying to do. private void SortByMemberName<T>(List<T> list, string memberName,

Is there a more elegant way to add nullable ints?

白昼怎懂夜的黑 提交于 2019-11-29 05:25:38
I need to add numerous variables of type nullable int. I used the null coalescing operator to get it down to one variable per line, but I have a feeling there is a more concise way to do this, e.g. can't I chain these statements together somehow, I've seen that before in other code. 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 +

Possible to overload null-coalescing operator?

戏子无情 提交于 2019-11-29 05:24:26
Is it possible to overload the null-coalescing operator for a class in C#? Say for example I want to return a default value if an instance is null and return the instance if it's not. The code would look like something like this: return instance ?? new MyClass("Default"); But what if I would like to use the null-coalescing operator to also check if the MyClass.MyValue is set? Good question! It's not listed one way or another in the list of overloadable and non-overloadable operators and nothing's mentioned on the operator's page . So I tried the following: public class TestClass { public

C# null coalescing operator equivalent for c++

主宰稳场 提交于 2019-11-29 03:06:32
Is there a C++ equivalent for C# null coalescing operator? I am doing too many null checks in my code. So was looking for a way to reduce the amount of null code. There isn't a way to do this by default in C++, but you could write one: in C# the ?? operator is defined as a ?? b === (a != null ? a : b) So, the C++ method would look like Coalesce(a, b) // put your own types in, or make a template { return a != null ? a : b; } adamjhilton I just found this: The ?? operator aka the Null Coalescing Operator You also have it in C/C++ as a GNU extension using the ?: operator : string pageTitle =

C# ?? operator in Ruby?

不问归期 提交于 2019-11-29 02:49:50
Is it possible to implement the ?? operator in Ruby? a = nil b = 1 x = a ?? b # x should == 1 x = b ?? 2 # x should == 1 You're looking for conditional assignment: a ||= b # Assign if a isn't already set and the || operator a = b || 2 # Assign if b is assigned, or assign 2 In Ruby, the short-circuiting Boolean operators ( || , && , and and or ) do not return true or false , but rather the first operand that determines the outcome of the entire expression. This works, because Ruby has a rather simple idea of truth. Or rather, it has a rather simple idea of falsehood: nil is false, and obviously

null conditional operator not working with nullable types?

五迷三道 提交于 2019-11-29 00:49:42
I'm writing a piece of code in c#6 and for some strange reason this works var value = objectThatMayBeNull?.property; but this doesn't: int value = nullableInt?.Value; By not works I mean I get a compile error saying Cannot resolve symbol 'Value' . Any idea why the null conditional operator ?. isn't working? Okay, I have done some thinking and testing. This is what happens: int value = nullableInt?.Value; Gives this error message when compiling: Type 'int' does not contain a definition for `Value' That means that ? 'converts' the int? into the actual int value. This is effectively the same as:

?? Coalesce for empty string?

爱⌒轻易说出口 提交于 2019-11-28 16:01:41
Something I find myself doing more and more is checking a string for empty (as in "" or null) and a conditional operator. A current example: s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber; This is just an extension method, it's equivalent to: string.IsNullOrEmpty(s.SiteNumber) ? "No Number" : s.SiteNumber; Since it's empty and not null, ?? won't do the trick. A string.IsNullOrEmpty() version of ?? would be the perfect solution. I'm thinking there has to be a cleaner way of doing this (I hope!), but I've been at a loss to find it. Does anyone know of a better way to do this, even if

Null-coalescing operator returning null for properties of dynamic objects

不羁岁月 提交于 2019-11-28 13:23:52
I have recently found a problem with the null-coalescing operator while using Json.NET to parse JSON as dynamic objects. Suppose this is my dynamic object: string json = "{ \"phones\": { \"personal\": null }, \"birthday\": null }"; dynamic d = JsonConvert.DeserializeObject(json); If I try to use the ?? operator on one of the field of d, it returns null: string s = ""; s += (d.phones.personal ?? "default"); Console.WriteLine(s + " " + s.Length); //outputs 0 However, if I assign a the dynamic property to a string, then it works fine: string ss = d.phones.personal; string s = ""; s += (ss ??

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

元气小坏坏 提交于 2019-11-28 11:57:21
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#? 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) 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(Function(y) Not IsNothing(y)) End Function just for reference, Coalesce operator for String Private Function Coalesce