null-coalescing-operator

null coalescing operator in accessor method

橙三吉。 提交于 2019-12-13 08:59:28
问题 i was looking around in stackoverflow whether putting null coalescing operators within an accessor method has any performance implications. Before: private Uri _Url; public Uri Url { if(_Url == null) _Url = new Uri(Utilities.GenerateUri()); return _Url; } After: private Uri _Url; public Uri Url { get { return _Url = _Url ?? new Uri(Utilities.GenerateUri()); } } I'm not even sure if the syntax is correct, but when i debug, the private object is set. Before anyone ask what's the point of doing

Why doesn't PHP's null coalescing operator (??) work on class constants with different visibilities?

自作多情 提交于 2019-12-12 12:22:35
问题 Consider the example below. Class a has private const SOMETHING , but class b has protected const SOMETHING . class a { private const SOMETHING = 'This is a!'; public static function outputSomething() { return static::SOMETHING ?? self::SOMETHING; } } class b extends a { protected const SOMETHING = 'This is b!'; } echo (new b())::outputSomething(); Output: This is b! But now if I comment out the definition for SOMETHING in class b, an error is thrown: class a { private const SOMETHING = 'This

how do I treat null lists like empty lists in linq?

回眸只為那壹抹淺笑 提交于 2019-12-10 13:08:33
问题 Below is some linqpad test code. When this runs it errors because the second instance of "item" has a null list of subitems as opposed to an empty list. I want to treat both situations (null or empty list) in exactly the same way but I wondered if there was a cleaner way than just putting a null check on the list and initialising an empty list when there's a null. in other words, I could do this: from si in (i.subitems == null ? new List<item>() : i.subitems) but that's a little ugly and I

Negate the null-coalescing operator

妖精的绣舞 提交于 2019-12-09 14:29:14
问题 I have a bunch of strings I need to use .Trim() on, but they can be null. It would be much more concise if I could do something like: string endString = startString !?? startString.Trim(); Basically return the part on the right if the part on the left is NOT null, otherwise just return the null value. I just ended up using the ternary operator, but is there anyway to use the null-coalescing operator for this purpose? 回答1: You could create an extension method which returns null when it tries

C# coalesce operator

爱⌒轻易说出口 提交于 2019-12-09 03:36:18
问题 Why the following code returns false? public static void Main() { bool? someCondition = true; bool someConditionOverride = false; bool? result = someCondition ?? someConditionOverride ? false : (bool?)null; Console.WriteLine(result); } I was exprecting the result will be true , since someCondition is not null and ?? operator will return true . However looks like right operand is calculated first and the left part is simply ignored. Adding brackets fix the confusion: bool? result =

Bad Use of Null Coalescing Operator?

我的梦境 提交于 2019-12-08 16:41:45
问题 myFoo = myFoo ?? new Foo(); instead of if (myFoo == null) myFoo = new Foo(); Am I correct in thinking that the first line of code will always perform an assignment? Also, is this a bad use of the null-coalescing operator? 回答1: I compared the CIL of the generated code (making sure to do a Release build - with Optimize Code checked in the Project Properties, which corresponds to the /optimize switch on csc.exe ). This is what I got (using VS 2008 - note that Foo.MaybeFoo() is a method that

null coalescing operator assignment to self

萝らか妹 提交于 2019-12-07 01:53:43
问题 I currently have two scripts set up in Unity to handle some UI Audio. One is a manager and the other is there to play a sound for a specific UI element. A simplified version of what I have is this: public class AudioUIManager : MonoBehaviour //Only one of these in the scene { public AudioClip genericUISound; //This is set in the inspector. } public class AudioUITextAnimation : MonoBehaviour { [SerializeField] private AudioClip specifiedUISound; //This is not set in the inspector

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

被刻印的时光 ゝ 提交于 2019-12-06 05:24:52
问题 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 =>

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

null coalescing operator assignment to self

这一生的挚爱 提交于 2019-12-05 06:00:31
I currently have two scripts set up in Unity to handle some UI Audio. One is a manager and the other is there to play a sound for a specific UI element. A simplified version of what I have is this: public class AudioUIManager : MonoBehaviour //Only one of these in the scene { public AudioClip genericUISound; //This is set in the inspector. } public class AudioUITextAnimation : MonoBehaviour { [SerializeField] private AudioClip specifiedUISound; //This is not set in the inspector [SerializeField] private AudioUIManager audioUIManager; // I get a reference to this elsewhere void Start() { //Use