null-coalescing-operator

c# shorthand for if not null then assign value

梦想与她 提交于 2021-02-15 05:59:24
问题 Is there any shorthand in c# now that will cutdown the following code: var testVar1 = checkObject(); if (testVar1 != null) { testVar2 = testVar1; } In this situation only want to assign testVar2 if testVar1 is not null from the CheckObject() result (testVar2 has a setter that will fire off code). Was trying to think how could use the null coalesce stuff but not really working out. Adding on to this testVar2 has code on it's setter to fire, so do not want testVar2 being set to anything if the

What does a double question mark do in C#? [duplicate]

狂风中的少年 提交于 2021-02-04 09:32:25
问题 This question already has answers here : Closed 11 years ago . Possible Duplicates: ?? Null Coalescing Operator --> What does coalescing mean? What do two question marks together mean in C#? I couldn't find this question being asked here so I figured I would ask it. What does a double question mark do in C#? Example: x = y ?? z; 回答1: This is a null coalescing operator. The method above states x is assigned y's value, unless y is null, in which case it is assigned z's value. 回答2: From

Null-Coallescing Operator - Why Casting?

牧云@^-^@ 提交于 2020-01-24 05:38:05
问题 Can anyone please tell me why does the first of the following statements throws a compilation error and the second one does not? NewDatabase.AddInParameter(NewCommand, "@SomeString", DbType.String, SomeString ?? DBNull.Value); // <-- Throws compilation error! NewDatabase.AddInParameter(NewCommand, "@SomeString", DbType.String, (object)(SomeString) ?? DBNull.Value); // <-- Compiles! I tried other nullable types such as byte? and got the same result. Can anyone please tell me why do I need to

Is there an “opposite” to the null coalescing operator? (…in any language?)

坚强是说给别人听的谎言 提交于 2020-01-19 04:43:07
问题 null coalescing translates roughly to return x, unless it is null, in which case return y I often need return null if x is null, otherwise return x.y I can use return x == null ? null : x.y; Not bad, but that null in the middle always bothers me -- it seems superfluous. I'd prefer something like return x :: x.y; , where what follows the :: is evaluated only if what precedes it is not null . I see this as almost an opposite to null coalescence, kind of mixed in with a terse, inline null-check,

Is there an “opposite” to the null coalescing operator? (…in any language?)

為{幸葍}努か 提交于 2020-01-19 04:43:05
问题 null coalescing translates roughly to return x, unless it is null, in which case return y I often need return null if x is null, otherwise return x.y I can use return x == null ? null : x.y; Not bad, but that null in the middle always bothers me -- it seems superfluous. I'd prefer something like return x :: x.y; , where what follows the :: is evaluated only if what precedes it is not null . I see this as almost an opposite to null coalescence, kind of mixed in with a terse, inline null-check,

What is the ?[]? syntax in C#?

霸气de小男生 提交于 2019-12-31 08:54:09
问题 While I was studying the delegate which is actually an abstract class in Delegate.cs, I saw the following method in which I don't understand Why the return value uses ? though it's already a reference( class ) type ?[]? meaning on the parameter Could you explain? public static Delegate? Combine(params Delegate?[]? delegates) { if (delegates == null || delegates.Length == 0) return null; Delegate? d = delegates[0]; for (int i = 1; i < delegates.Length; i++) d = Combine(d, delegates[i]); return

How am I misusing the null-coalescing operator? Is this evaluating “null” correctly?

不打扰是莪最后的温柔 提交于 2019-12-30 23:49:31
问题 I am trying to use the null-coalescing operator in a C# script in Unity, my project Scripting Runtime is set to .NET 4.x so it should work correctly. The problem is that even though the LEFT operand evaluates to null, it does not correctly return the RIGHT operand. Here is an example statement that does NOT work when the left Operand returns null: m_meshFilter = ( GetMeshFilter() ?? AddMeshFilter() ); Here is the SAME exact statement, except it explicitly passes null to the null-coalescing

How am I misusing the null-coalescing operator? Is this evaluating “null” correctly?

a 夏天 提交于 2019-12-30 23:49:12
问题 I am trying to use the null-coalescing operator in a C# script in Unity, my project Scripting Runtime is set to .NET 4.x so it should work correctly. The problem is that even though the LEFT operand evaluates to null, it does not correctly return the RIGHT operand. Here is an example statement that does NOT work when the left Operand returns null: m_meshFilter = ( GetMeshFilter() ?? AddMeshFilter() ); Here is the SAME exact statement, except it explicitly passes null to the null-coalescing

C# Reflection get Field or Property by Name

牧云@^-^@ 提交于 2019-12-29 08:29:09
问题 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

Null-coalescing operator returning null for properties of dynamic objects

前提是你 提交于 2019-12-28 20:35:46
问题 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