Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 variable anymore.

It's not that I cannot use it like I have it already but I just want to know if there's any possibility to use the null-conditional operator on this.

like image 379
diiN__________ Avatar asked Mar 09 '16 09:03

diiN__________


People also ask

What is null conditional operator in C#?

Null-conditional operators ?. and ?[] Available in C# 6 and later, a null-conditional operator applies a member access, ?. , or element access, ?[] , operation to its operand only if that operand evaluates to non-null; otherwise, it returns null . That is, If a evaluates to null , the result of a?. x or a?[x] is null .

Is null C# operator?

Introduced in C# 6.0, the Null Conditional Operator ?. will immediately return null if the expression on its left-hand side evaluates to null , instead of throwing a NullReferenceException . If its left-hand side evaluates to a non- null value, it is treated just like a normal .

What is Elvis operator in C#?

In certain computer programming languages, the Elvis operator, often written ?: , or or || , is a binary operator that returns its first operand if that operand evaluates to a true value, and otherwise evaluates and returns its second operand.

What does the question mark mean in C#?

The conditional operator ?: , also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false , as the following example shows: C# Copy.


Video Answer


2 Answers

The null propagation operator returns a value. And since you must have a variable on the left hand side of an assignment, and not a value, you cannot use it in this way.

Sure, you could make things shorter by using the tenary operator, but that, on the other hand, doesn't really help the readability aspect.

Joachim Isaksson's comment on your question shows a different approach that should work.

like image 92
Allmighty Avatar answered Oct 11 '22 00:10

Allmighty


As Joachim Isaksson suggested in the comments, I now have a method SetData(Data data) and use it like this:

MyPage1?.SetData(this.data); MyPage2?.SetData(this.data); MyPage3?.SetData(this.data); 
like image 26
diiN__________ Avatar answered Oct 10 '22 23:10

diiN__________