2

Does C# have an equivalent to JavaScript's assignment syntax var x = y || z;? In case you don't know, the result is not true/false. If y is defined, then it is assigned to x, otherwise z is assigned to x even if it is undefined.

Note that in JavaScript the variable still has to be declared: var test;

3
  • 1
    "If y is defined, then it is assigned to x". Not quite. If y can be treated as a false value (0, false, null) it doesn't matter if it is defined or not; x will be assigned to the value held by z. - jsfiddle.net/8cMzK Commented May 9, 2013 at 19:53
  • @TimMedora Ah the magic of a non-statically typed language. Commented May 9, 2013 at 19:55
  • That's why it feels dangerous to equate it with null coalescing in c#...they behave similarly sometimes, but they are quite different in practice, partly due to fundamental differences in the languages. Commented May 9, 2013 at 19:57

6 Answers 6

7

I think that you are looking for ?? operator.

MSDN Reference

Sign up to request clarification or add additional context in comments.

1 Comment

While this will work for most things it will only work with nullables.
3
var abc = blah ?? "default";

yep

Comments

3

This is what you are looking for

var x = y ?? z;

Comments

1

In C# there's no such notion as variable not being defined. Such operator doesn't make sense in C#.

2 Comments

I'm not sure how you're defining defined, but the closest thing to Javascript's undefined status would be either null or unassigned. Probably null.
I know its not 1 to 1 definition of course but we are dealing with practicality and null is the default value assigned to objects that have not been instantiated which is the same for JavaScript with undefined. But you are correct none the less.
1

Unlike JavaScript, C# is not dynamic but static language so that such operation is impossible - a compilation error will occur.

Imagine you're writing this if:

if(pizzaPrice == hamburgerPrice)

Before declaring the variables first:

decimal pizzaPrice;
decimal hamburgerPrice;

An error will occur on compile-time.

Update: Even if the variables were declared it doesn't matter because C# does not support such a feature.

On the other hand, JavaScript is enforcing evaluation of the variable in if conditions by calling the ToBoolean method and if it's undefined or null it's equals to false and C# doesn't not contains such a behavior.

Look at this cool article: JavaScript pitfalls: null, false, undefined, NaN

But if you want to check if a variable is referencing to a null you can easily use the null coalescing operator "??" operator.

As the following:

var x = y ?? z;

2 Comments

The variable still has to be declared. I should have mentioned that earlier.
@ArlenBeiler Please read my update in the answer. Generally, JavaScript evaluates null or undefined variables as false in if conditions!
0

Yes, there is: ??

string x = y ?? z;

Which basically calculates:

string x = y != null ? y : z

However, there are a few differences between Javascript and C#. As with JS, y and z must both be declared before hand. However, unlike JS, y and z must also be "assigned" in C# or a compiler error will be thrown as usual.

The operator requires a nullable type and it checks whether the first is null before returning the second. You can chain a whole bunch (a ?? b ?? c ?? d ?? e) if you want.

Note that a zero length string is not null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.