JoshBleggi
JoshBleggi

Reputation: 101

Is there a conditional operator for setting a variable in C# to the value of another variable if the latter isn't null?

Something like the ternary operator (?:) or the null coalescing operator (??). It seems silly to me to take up two lines and be so wordy when the other operators exist.

EDIT: Since it's requested, here's two possible examples of what I hope that I can find

var variable ?= mightBeNull;

or

var variable = mightBeNull != null ? mightBeNull

Really, it's either something that can be used to assign a value, if it's not null, to a variable or an If without an Else packaged nicely in an operator.

Upvotes: 1

Views: 17528

Answers (6)

Abubakar Ahmad
Abubakar Ahmad

Reputation: 2583

I bet this is what you are looking for.

Let's say we have a function, myFunction that says whether the argument passed input is null or not. If input is null, it will return "Input is null!", else it will return "Input is not null".

This is the normal approach:

public String myFunction(string input) {  
    if (input == null)  
        return "Input is null!";  
    else  
        return "Input is not null";
}

And this is the other approach (What you are looking for):

public String myFunction(string input) {
    return (input == null) ? "Input is null!" : "Input is not null";
}

It is called (?: conditional operator).

Upvotes: 2

C. McCoy IV
C. McCoy IV

Reputation: 897

The ??= operator is coming to C# 8.

int? i = null; // i = null

i ??= 0; // i = 0
i ??= 1; // i = 0

// different ways of writing 'i ??= 0;':
i = i ?? 0;
if (i == null) i = 0;

Upvotes: 4

John Aho
John Aho

Reputation: 31

In Visual Studio 2015 C# there is a new operator called the Null-Conditional that does what you are asking. It is the ?. or ? operator.

int? length = customers?.Length; // null if customers is null 
Customer first = customers?[0];  // null if customers is null
int? count = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null

Currently useable in previous versions of C# but not an operator but rather a one liner would be to use the ? : statement ?: operator

condition ? first_expression : second_expression;
AssingningTO = (someVar == null) ?  null: someVar; // if null assign null ELSE assign someVar
AssingningTO = (someVar == null) ?  String.Empth(): someVar; // if null assign empty string else someVar

Upvotes: 0

Tim S.
Tim S.

Reputation: 56576

So you want this?

if (other != null)
    someVariable = other;

You could do the following, but I'd argue that the above is better due to clarity and possible side effects:

someVariable = other ?? someVariable;

The above might cause side effects if someVariable is a property and either the get or set can cause side effects; this shouldn't be important if your property follows the ordinary guidelines. Or, as Servy points out, even if it's a field, it could created different semantics in a multithreaded app. I should note that in the first example, you read other twice, which also has the potential for complexity to enter (though a smaller potential than the latter example).

Upvotes: 2

Servy
Servy

Reputation: 203812

To assign a value to a variable only if it is not null, you would need to use an if. Neither the conditinal nor the null coalesce operators would do that.

if(somethingElse != null) something = somethingElse;

There is no specific operator in C# that does exactly this.

Upvotes: 1

Sheekha
Sheekha

Reputation: 1

Nullable types can represent all the values of an underlying type, and an additional null value.

http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx

Upvotes: -4

Related Questions