null-conditional-operator

Trying to understand ?. (null-conditional) operator in C#

倖福魔咒の 提交于 2019-12-04 14:55:48
问题 I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { System.Console.WriteLine("Contains elements"); } A a = null; if (a?.B) { System.Console.WriteLine("Is initialized"); } } } The line if (list?.Count > 0) compiles perfectly which means that if list is null , the expression Count > 0 becomes false by default. However, the line if (a?.B) throws a compiler error saying I can't implicitly

Trying to understand ?. (null-conditional) operator in C#

北城余情 提交于 2019-12-03 09:17:11
I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { System.Console.WriteLine("Contains elements"); } A a = null; if (a?.B) { System.Console.WriteLine("Is initialized"); } } } The line if (list?.Count > 0) compiles perfectly which means that if list is null , the expression Count > 0 becomes false by default. However, the line if (a?.B) throws a compiler error saying I can't implicitly convert bool? to bool . Why is one different from the other? Heinzi list?.Count > 0 : Here you compare

UnassignedReferenceException even though using the null-conditional operator

假如想象 提交于 2019-12-02 19:30:47
问题 I'm getting a UnassignedReferenceException: The variable _Preset of Foo has not been assigned. even though I'm using the null-conditional operator ?. . My code: // […] myTarget.Preset?.ApplyTo(myTarget); I'm also noticing that it mentions _Preset instead of Preset (which I find odd). Code in Foo.cs : [CreateAssetMenu()] public class Foo : ScriptableObject { [SerializeField] private Preset _Preset = null; public Preset Preset { get { return _Preset; } protected set { _Preset = value; } } }

UnassignedReferenceException even though using the null-conditional operator

时光总嘲笑我的痴心妄想 提交于 2019-12-02 10:57:18
I'm getting a UnassignedReferenceException: The variable _Preset of Foo has not been assigned. even though I'm using the null-conditional operator ?. . My code: // […] myTarget.Preset?.ApplyTo(myTarget); I'm also noticing that it mentions _Preset instead of Preset (which I find odd). Code in Foo.cs : [CreateAssetMenu()] public class Foo : ScriptableObject { [SerializeField] private Preset _Preset = null; public Preset Preset { get { return _Preset; } protected set { _Preset = value; } } } What am I doing wrong? Isn't it what the operator is for? Google searches didn't help. Unity has a custom

Is C# 6 ?. (Elvis op) thread safe? If so, how?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 14:35:51
Apologies in advance: this question comes from a hard-core, unreformed C++ developer trying to learn advanced C#. Consider the following: if (myUserDefinedObject != null) { myUserDefinedObject.ToString(); } This is obviously not thread safe. On the other hand, I've seen two tutorials that say ?. (the Null Conditional Operator or 'Elvis Operator') for example, myUserDefinedObject?.ToString(); IS thread safe. Unless the compiler wraps a [mutex?] lock around it under the covers (shiver), I don't understand how that can be true. If this idiom is thread safe, can someone point me to a technical

Using the null-conditional operator on the left-hand side of an assignment

妖精的绣舞 提交于 2019-11-27 09:11:42
I have a few pages, each with a property named Data . On another page I'm setting this data like this: if (MyPage1 != null) MyPage1.Data = this.data; if (MyPage2 != null) MyPage2.Data = this.data; if (MyPage3 != null) MyPage3.Data = this.data; Is there any possibility to use the null-conditional operator on MyPage ? I'm thinking of something like this: MyPage?.Data = this.data; But when I write it like this, I get the following error: The left-hand side of an assignment must be a variable, property or indexer. I know it's because MyPage could be null and the left-hand side wouldn't be a

Is C# 6 ?. (Elvis op) thread safe? If so, how?

[亡魂溺海] 提交于 2019-11-26 16:49:13
问题 Apologies in advance: this question comes from a hard-core, unreformed C++ developer trying to learn advanced C#. Consider the following: if (myUserDefinedObject != null) { myUserDefinedObject.ToString(); } This is obviously not thread safe. On the other hand, I've seen two tutorials that say ?. (the Null Conditional Operator or 'Elvis Operator') for example, myUserDefinedObject?.ToString(); IS thread safe. Unless the compiler wraps a [mutex?] lock around it under the covers (shiver), I don't

C# elegant way to check if a property's property is null

不打扰是莪最后的温柔 提交于 2019-11-26 15:14:21
In C#, say that you want to pull a value off of PropertyC in this example and ObjectA, PropertyA and PropertyB can all be null. ObjectA.PropertyA.PropertyB.PropertyC How can I get PropertyC safely with the least amount of code? Right now I would check: if(ObjectA != null && ObjectA.PropertyA !=null && ObjectA.PropertyA.PropertyB != null) { // safely pull off the value int value = objectA.PropertyA.PropertyB.PropertyC; } It would be nice to do something more like this (pseudo-code). int value = ObjectA.PropertyA.PropertyB ? ObjectA.PropertyA.PropertyB : defaultVal; Possibly even further

Using the null-conditional operator on the left-hand side of an assignment

让人想犯罪 __ 提交于 2019-11-26 14:36:28
问题 I have a few pages, each with a property named Data . On another page I'm setting this data like this: if (MyPage1 != null) MyPage1.Data = this.data; if (MyPage2 != null) MyPage2.Data = this.data; if (MyPage3 != null) MyPage3.Data = this.data; Is there any possibility to use the null-conditional operator on MyPage ? I'm thinking of something like this: MyPage?.Data = this.data; But when I write it like this, I get the following error: The left-hand side of an assignment must be a variable,

C# elegant way to check if a property's property is null

放肆的年华 提交于 2019-11-26 03:09:18
问题 In C#, say that you want to pull a value off of PropertyC in this example and ObjectA, PropertyA and PropertyB can all be null. ObjectA.PropertyA.PropertyB.PropertyC How can I get PropertyC safely with the least amount of code? Right now I would check: if(ObjectA != null && ObjectA.PropertyA !=null && ObjectA.PropertyA.PropertyB != null) { // safely pull off the value int value = objectA.PropertyA.PropertyB.PropertyC; } It would be nice to do something more like this (pseudo-code). int value