null-coalescing-operator

Is “If” condition better than ?? and casting

╄→гoц情女王★ 提交于 2019-12-05 01:52:23
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 answers, following are the benefits of ?? Increased readability Decreased branching deapth of program flow

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

百般思念 提交于 2019-12-05 00:14:38
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 Callback variable becoming stale. I'm worried about an Exception being thrown from DoCallback . EDIT #2

Is the C# '??' operator thread safe?

ⅰ亾dé卋堺 提交于 2019-12-04 20:04:07
问题 Everyone knows that this is not thread safe: public StringBuilder Builder { get { if (_builder != null) _builder = new StringBuilder(); return _builder; } } What about this? public StringBuilder Builder { get { return _builder ?? (_builder = new StringBuilder()); } } 回答1: BEGIN EDIT Based on your edited title, the null-coalescing operator itself seems to be thread-safe (see Phil Haack's analysis). It appears, however, that it doesn't guarantee against the potential multiple calls to the

Ternary/null coalescing operator and assignment expression on the right-hand side?

房东的猫 提交于 2019-12-04 10:37:46
While experimenting with ternary and null coalesce operators in C# I discovered that it is possible to use assignments on the right-hand side of expressions, for example this is a valid C# code: int? a = null; int? b = null; int? c = a ?? (b = 12); int? d = a == 12 ? a : (b = 15); Strangely enough, not only the assignment on the right-hand side of the expression is evaluated to its own right-hand side (meaning that the third line here is evaluated to 12 and not to something like b = 12 => void ), but this assignment also effectively works, so that two variables are assigned in one statement.

Coalesce operator in C#?

这一生的挚爱 提交于 2019-12-03 16:58:38
问题 I think i remember seeing something similar to the ?: ternary operator in C# that only had two parts to it and would return the variable value if it wasn't null and a default value if it was. Something like this: tb_MyTextBox.Text = o.Member ??SOME OPERATOR HERE?? "default"; Basically the equivalent of this: tb_MyTextBox.Text = o.Member != null ? o.Member : "default"; Does such a thing exist or did I just imagine seeing this somewhere? 回答1: Yup: tb_myTextBox.Text = o.Member ?? "default"; http

Is the C# '??' operator thread safe?

牧云@^-^@ 提交于 2019-12-03 13:08:47
Everyone knows that this is not thread safe: public StringBuilder Builder { get { if (_builder != null) _builder = new StringBuilder(); return _builder; } } What about this? public StringBuilder Builder { get { return _builder ?? (_builder = new StringBuilder()); } } BEGIN EDIT Based on your edited title, the null-coalescing operator itself seems to be thread-safe (see Phil Haack's analysis ). It appears, however, that it doesn't guarantee against the potential multiple calls to the StringBuilder constructor. END EDIT You have a larger problem with threading, and that is that the Builder

Coalesce operator in C#?

拟墨画扇 提交于 2019-12-03 06:05:07
I think i remember seeing something similar to the ?: ternary operator in C# that only had two parts to it and would return the variable value if it wasn't null and a default value if it was. Something like this: tb_MyTextBox.Text = o.Member ??SOME OPERATOR HERE?? "default"; Basically the equivalent of this: tb_MyTextBox.Text = o.Member != null ? o.Member : "default"; Does such a thing exist or did I just imagine seeing this somewhere? Yup: tb_myTextBox.Text = o.Member ?? "default"; http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx Well, it's not quite the same as the conditional

In your opinion what is more readable: ?? (operator) or use of if's

萝らか妹 提交于 2019-12-02 00:30:40
问题 I have a method that will receive a string , but before I can work with it, I have to convert it to int . Sometimes it can be null and I have to change its value to "0" . Today I have: public void doSomeWork(string value) { int SomeValue = int.Parse(value ?? "0"); //it can throw an exception(i know) } I did it, but my boss asked me to refactor it to: public void doSomeWork(string value) { if(string.IsNullOrEmpty(value)) value = "0"; int SomeValue = int.Parse(value); } in your opinion what is

In your opinion what is more readable: ?? (operator) or use of if's

99封情书 提交于 2019-12-01 20:29:21
I have a method that will receive a string , but before I can work with it, I have to convert it to int . Sometimes it can be null and I have to change its value to "0" . Today I have: public void doSomeWork(string value) { int SomeValue = int.Parse(value ?? "0"); //it can throw an exception(i know) } I did it, but my boss asked me to refactor it to: public void doSomeWork(string value) { if(string.IsNullOrEmpty(value)) value = "0"; int SomeValue = int.Parse(value); } in your opinion what is the best option? Personally I'd go for the corrected version of your bosses - possibly with even more

Null coalesce operator with casting

点点圈 提交于 2019-12-01 19:28:00
问题 I have upgraded to PHP 7 and started using the null coalesce operator to convert things like $email = isset($_SESSION['email']) ? $_SESSION['email'] : ''; to $email = $_SESSION['email'] ?? ''; but I can't figure out how to do this if I am casting as well. For example, the for the old statement $id = isset($_GET['id']) ? (int) $_GET['id'] : 0; I would think something like $id = (int) $_GET['id'] ?? 0; should work, but it doesn't appear to in the sense that, if $_GET['id'] is not set, $id