1

In javascript I can do this

var value = obj.A || 3;

which means, if obj.A is null or undefined or empty, then use 3 instead.

How can I do that in c#? Or is this the best way in c#?

int value = obj.A == null ? 3 : obj.A; 
8
  • 8
    Use the null-coalescing operator: ??. However this only works on null values not "empty" or other "default" values, only null Commented Sep 27, 2018 at 14:33
  • In other words int value = obj.A?? 3 Commented Sep 27, 2018 at 14:34
  • int value = obj == null ? 3 : obj.A Commented Sep 27, 2018 at 14:35
  • 3
    @Devid that is the same code OP posted though? Commented Sep 27, 2018 at 14:36
  • @Devid that doesn't solve the main issue, which is straight from the OP: if *obj.A* is null or undefined or empty, then use 3 instead. Commented Sep 27, 2018 at 14:37

2 Answers 2

4

You can do this using null propagation ?. and the null coalescing operator ??.

var value = obj?.A ?? 3;

The first one says "if obj is null, ignore A and just use null". The second one says "if the left-hand side is null, use the right hand side"

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

4 Comments

I just noticed I answered the wrong question. This works if obj OR A are null. It won't check if A has never been defined. C# doesn't have the same concept of 'undefined' as JavaScript and will default int properties to 0
But it would work if .A was of type int?, which is the closest C# can come
Instead of var i suggest to use the real type int for clarity
This doesn't check for empty though. So if obj.A is 0 (for int) or empty string ("" for string) then that will be set instead of 3.
0

... if obj.A is null or undefined or empty, then use 3 instead.

c# has no concept of undefined. There is not much you can do about this, if you want an equivalent you would have to create some sort of generic type wrapper but I would advise against it.

There is also no native truthy check (which is what you are actually referring to when you wrote "empty") in c# like there is in javascript. You would have to build your own method or extension method or add an additional check against the default value in comparison that you want to do.

class Temp
{
    public int? A {get;set;}
}

public static void Main()
{
    Temp obj = new Temp();

    int resultingValue1 = (obj.A == null  || obj.A == default(int)) ? 3 : (int) obj.A;
    // or
    int resultingValue2 = obj.A.IsNullEmpty() ? 3 : (int) obj.A;
}
public static class Extensions
{
    public static bool IsNullEmpty<T>(this T valueToCheck) 
    {
        return valueToCheck == null || EqualityComparer<T>.Default.Equals(valueToCheck, default(T));
    }
}

See also Marc Gravell's answer Null or default comparison of generic argument in C#

Fiddle

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.