null-coalescing

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

How to print the null values if the dictionary has it using LINQ

假如想象 提交于 2020-07-22 05:58:04
问题 I have dictionary with the following keys and values. I am trying to print the dictionary, but nothing prints where there is a null value. How do I print "null" in the output? Dictionary<string, object> dic1 = new Dictionary<string, object>(); dic1.Add("id", 1); dic1.Add("name", "john"); dic1.Add("grade", null); Console.WriteLine(string.Join(Environment.NewLine, dic1.Select(a => $"{a.Key}: {a.Value}"))); Here is the output I get: id: 1 name: john grade: 回答1: You can use the null-coalescing

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

Null-coalescing out parameter gives unexpected warning

南笙酒味 提交于 2019-12-30 18:39:33
问题 Using this construct: var dict = new Dictionary<int, string>(); var result = (dict?.TryGetValue(1, out var value) ?? false) ? value : "Default"; I get an error saying CS0165 use of unassigned local variable 'value' which is not what I expect. How could value possibly be undefined? If the dictionary is null the inner statement will return false which will make the outer statement evaluate to false, returning Default . What am I missing here? Is it just the compiler being unable to evaluate the

IEnumerable<T> null coalescing Extension

本秂侑毒 提交于 2019-12-29 08:42:28
问题 I frequently face the problem to check whether an IEnumerable<T> is null before iterating over it through foreach or LINQ queries, then I often come into codes like this: var myProjection = (myList ?? Enumerable.Empty<T>()).Select(x => x.Foo)... Hence, I thought to add this extension-method to an Extensions class: public static class MyExtensions { public static IEnumerable<T> AsEmptyIfNull<T>(this IEnumerable<T> source) { return source ?? Enumerable.Empty<T>(); } } A little issue comes

Sending null parameters to Sql Server

北战南征 提交于 2019-12-22 08:57:38
问题 I have a SqlCommand object that I'm using to update a database table but it doesn't interpret my null values correctly. Here is the SQL: UPDATE dbo.tbl SET param1 = @param1, param2 = @param2, param3 = @param3, param4 = @param4, param5 = @param5, param6 = @param6, param7 = @param7, param8 = @param8, param9 = @param9, param10 = @param10 WHERE param11 = @param11 I have tried null coalescing parameters that are nullable like this, but I haven't had any success. Unless otherwise noted, all

Sending null parameters to Sql Server

空扰寡人 提交于 2019-12-05 19:29:42
I have a SqlCommand object that I'm using to update a database table but it doesn't interpret my null values correctly. Here is the SQL: UPDATE dbo.tbl SET param1 = @param1, param2 = @param2, param3 = @param3, param4 = @param4, param5 = @param5, param6 = @param6, param7 = @param7, param8 = @param8, param9 = @param9, param10 = @param10 WHERE param11 = @param11 I have tried null coalescing parameters that are nullable like this, but I haven't had any success. Unless otherwise noted, all parameters are strings: command.Parameters.AddWithValue("@param1", param1 ?? DBNull.Value); command.Parameters

IEnumerable<T> null coalescing Extension

女生的网名这么多〃 提交于 2019-11-29 14:18:11
I frequently face the problem to check whether an IEnumerable<T> is null before iterating over it through foreach or LINQ queries, then I often come into codes like this: var myProjection = (myList ?? Enumerable.Empty<T>()).Select(x => x.Foo)... Hence, I thought to add this extension-method to an Extensions class: public static class MyExtensions { public static IEnumerable<T> AsEmptyIfNull<T>(this IEnumerable<T> source) { return source ?? Enumerable.Empty<T>(); } } A little issue comes immediately in my mind looking at this code, i.e., given the "instance-methods aspect" of extension-methods,

Null Coalescing Operator in F#?

▼魔方 西西 提交于 2019-11-27 19:24:46
When interacting with C# libraries, I find myself wanting C#'s null coalescing operator both for Nullable structs and reference types. Is it possible to approximate this in F# with a single overloaded operator that inlines the appropriate if case? Yes, using some minor hackery found in this SO answer " Overload operator in F# ". At compiled time the correct overload for an usage of either ('a Nullable, 'a) ->'a or ('a when 'a:null, 'a) -> 'a for a single operator can be inlined. Even ('a option, 'a) -> 'a can be thrown in for more flexibility. To provide closer behavior to c# operator, I've