null-coalescing-operator

Which works faster Null coalesce , Ternary or If Statement [closed]

风流意气都作罢 提交于 2019-12-28 18:05:14
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . We use ?? Operator to evaluate expressions against null values, for example: string foo = null; string bar = "woooo"; string foobar= foo ?? bar ; // Evaluates foobar as woooo We also used an if statement, which

C#'s null coalescing operator (??) in PHP

岁酱吖の 提交于 2019-12-27 23:37:07
问题 Is there a ternary operator or the like in PHP that acts like ?? of C#? ?? in C# is clean and shorter, but in PHP you have to do something like: // This is absolutely okay except that $_REQUEST['test'] is kind of redundant. echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi'; // This is perfect! Shorter and cleaner, but only in this situation. echo null? : 'replacement if empty'; // This line gives error when $_REQUEST['test'] is NOT set. echo $_REQUEST['test']?: 'hi'; 回答1: PHP 7 adds the

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

Is “If” condition better than ?? and casting

佐手、 提交于 2019-12-22 03:49:04
问题 I have following two approaches for same functionality - one with "if” condition and one with "?? and casting". Which approach is better? Why? Code: Int16? reportID2 = null; //Other code //Approach 1 if (reportID2 == null) { command.Parameters.AddWithValue("@report_type_code", DBNull.Value); } else { command.Parameters.AddWithValue("@report_type_code", reportID2); } //Approach 2 command.Parameters.AddWithValue("@report_type_code", ((object) reportID2) ?? DBNull.Value); UPDATE Based on the

Is the null coalescing operator (??) in C# thread-safe?

只谈情不闲聊 提交于 2019-12-22 03:20:13
问题 Is there a race condition in the following code that could result in a NullReferenceException ? -- or -- Is it possible for the Callback variable to be set to null after the null coalescing operator checks for a null value but before the function is invoked? class MyClass { public Action Callback { get; set; } public void DoCallback() { (Callback ?? new Action(() => { }))(); } } EDIT This is a question that arose out of curiosity. I don't normally code this way. I'm not worried about the

Does null coalescing operator cache the result in c#

自作多情 提交于 2019-12-19 14:57:12
问题 I know that doing (myValue ?? new SomeClass()) is similar to (myValue == null ? new SomeClass() : myValue) But out of curiosity, is there any performance benefit when I call a function, say (getResult() ?? new SomeClass()) . Will getResult() get executed twice? It seems unintuitive since I've specified the method call only once. 回答1: Well, if by "caching" you mean storing it in a temporary variable, then yes. This construct: var result = (getResult() ?? new SomeClass()); can be thought of to

Why doesn't the null coalescing operator (??) work in this situation?

泄露秘密 提交于 2019-12-19 05:11:28
问题 I'm getting an unexpected NullReferenceException when I run this code, omitting the fileSystemHelper parameter (and therefore defaulting it to null): public class GitLog { FileSystemHelper fileSystem; /// <summary> /// Initializes a new instance of the <see cref="GitLog" /> class. /// </summary> /// <param name="pathToWorkingCopy">The path to a Git working copy.</param> /// <param name="fileSystemHelper">A helper class that provides file system services (optional).</param> /// <exception cref

null conditional operator not working with nullable types?

房东的猫 提交于 2019-12-18 03:00:23
问题 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? 回答1: 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

VB.NET null coalescing operator? [duplicate]

柔情痞子 提交于 2019-12-17 15:59:15
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Coalesce operator and Conditional operator in VB.NET Is there a VB.NET equivalent for C#'s ?? operator? Is there a built-in VB.NET equivalent to the C# null coalescing operator? 回答1: Yes, there is, a long as you're using VB 9 or later (included with Visual Studio 2008). You can use the version of the If operator overloaded to accept only two arguments: Dim myVar? As Integer = Nothing Console.WriteLine(If(myVar,

Is there a VB.NET equivalent for C#'s '??' operator?

此生再无相见时 提交于 2019-12-17 02:26:08
问题 Is there a VB.NET equivalent for C#'s ?? operator? 回答1: Use the If() operator with two arguments (Microsoft documentation): ' Variable first is a nullable type. Dim first? As Integer = 3 Dim second As Integer = 6 ' Variable first <> Nothing, so its value, 3, is returned. Console.WriteLine(If(first, second)) second = Nothing ' Variable first <> Nothing, so the value of first is returned again. Console.WriteLine(If(first, second)) first = Nothing second = 6 ' Variable first = Nothing, so 6 is