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; 
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
omega
  • 40,311
  • 81
  • 251
  • 474
  • 8
    Use the [null-coalescing operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator): `??`. However this only works on null values not "empty" or other "default" values, _only null_ – maccettura Sep 27 '18 at 14:33
  • In other words int value = obj.A?? 3 – CodeHacker Sep 27 '18 at 14:34
  • `int value = obj == null ? 3 : obj.A` – Devid Sep 27 '18 at 14:35
  • 3
    @Devid that is the same code OP posted though? – maccettura Sep 27 '18 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`. – Drew Kennedy Sep 27 '18 at 14:37
  • In my opinion OP has already figured out the most similar way to do it in C#. – Devid Sep 27 '18 at 14:39
  • There is no native `truthy` check in c# like there is in javascript. You would have to build your own or add an additional check against the default value. `int value = (obj.A == null || obj.A == default(int)) ? 3 : obj.A;` – Igor Sep 27 '18 at 14:39
  • @Devid Thats a pretty false statement though. The most "similar" way is to use a null coalescing operator (assuming a check for null). If OP wants to check against arbitrary "falsey" values like Javascript, then _there is no built in way_ in C# – maccettura Sep 27 '18 at 14:40

2 Answers2

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"

Hofma Dresu
  • 432
  • 3
  • 13
  • 1
    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 – Hofma Dresu Sep 27 '18 at 14:45
  • 2
    But it would work if `.A` was of type `int?`, which is the closest C# can come – Hans Kesting Sep 27 '18 at 14:47
  • 1
    Instead of `var` i suggest to use the real type `int` for clarity – Tim Schmelter Sep 27 '18 at 14:47
  • 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. – Bivo Kasaju Sep 27 '18 at 15:23
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

Igor
  • 60,821
  • 10
  • 100
  • 175