8

In my code, is there a shorthand that I can use to assign a variable the value of a object's property ONLY if the object isn't null?

string username = SomeUserObject.Username;     // fails if null

I know I can do a check like if(SomeUserObject != null) but I think I saw a shorthand for this kind of test.

I tried:

string username = SomeUserObject ?? "" : SomeUserObject.Username;

But that doesn't work.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • On the second, you're thinking of both the null coalescing operator *and* the conditional operator. Unfortunately, you've managed to combine them into illegal code. See the answers below for usage of the conditional operator. – Anthony Pegram Apr 11 '10 at 06:04

7 Answers7

6

In c# 6.0 you can now do

string username = SomeUserObject?.Username;

username will be set to null if SomeUSerObject is null. If you want it to get the value "", you can do

string username = SomeUserObject?.Username ?? "";
Philip
  • 198
  • 1
  • 5
  • What if `username` is an existing variable with already a value and you want to overwrite the value ONLY if `SomeUserObject?.Username` is not null otherwise leave the existing value. Is there another shorter way than `username = SomeUserObject?.Username ?? username;` ? – Jérôme MEVEL Jun 14 '23 at 13:26
4

Your syntax on the second is slightly off.

string name = SomeUserObject != null ? SomeUserObject.Username : string.Empty;
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
2

The closest you're going to get, I think, is:

string username = SomeUserObject == null ? null : SomeUserObject.Username;
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
1

This is probably as close as you are going to get:

string username = (SomeUserObject != null) ? SomeUserObject.Username : null;
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
1

You can use ? : as others have suggested but you might want to consider the Null object pattern where you create a special static User User.NotloggedIn and use that instead of null everywhere.

Then it becomes easy to always do .Username.

Other benefits: you get / can generate different exceptions for the case (null) where you didn't assign a variable and (not logged in) where that user isn't allowed to do something.

Your NotloggedIn user can be a derived class from User, say NotLoggedIn that overrides methods and throws exceptions on things you can't do when not logged in, like make payments, send emails, ...

As a derived class from User you get some fairly nice syntactic sugar as you can do things like if (someuser is NotLoggedIn) ...

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
0

You're thinking of the ternary operator.

string username = SomeUserObject == null ? "" : SomeUserObject.Username;

See http://msdn.microsoft.com/en-us/library/ty67wk28.aspx for more details.

micahtan
  • 18,530
  • 1
  • 38
  • 33
-1

It is called null coalescing and is performed as follows:

string username = SomeUserObject.Username ?? ""
JP.
  • 5,536
  • 7
  • 58
  • 100