C# ?? null coalescing operator

?? ist ein Operator der schon in c#2.0 existiert aber ich erst heute davon höre!! Krass! Diese Zeile

int result2 = number == null ? 0 : (int)number;
wird zu
int result = number ?? 0;
SUPER ODER Gesamtes Beispiel:
int? number = null;
int result = number ?? 0; int result2 = number == null ? 0 : (int)number; Console.WriteLine("result: " + result); Console.WriteLine("result2: " + result2);
Link von Scott Gu

3 comments:

Anonymous said...

You can also chain these:

string blah = s ?? d ?? z ?? e;

Peter Gfader said...

thanks!

good to know.

Anonymous said...

Use that operator also for instantiating lists!!

private IList< BusinessEntity > _BusinessEntity;

public IList< BusinessEntity > ListOfBusinessEntity { get { return _BusinessEntity ?? (_BusinessEntity = new List< BusinessEntity >()); } }

Post a Comment

Latest Posts

Popular Posts