Shorthand C#

I've seen this really funky shorthand code in a blog post that i now can't remember.  It's been bugging me for a while now so i thought i'd ask.

Currently i have this: string answer = (obj =! null)? obj: " ";
It's something similar to string answer = (obj : " ");

I know i could write a method to handle this but i'm looking for the inbuilt way.

Any ideas?  Thanks  Also, if you know of any other cool little short hand tricks feel free to post =)
LVL 12
Who is Participating?
photowhizConnect With a Mentor Commented:
You might be thinking of the coalesce operator on nullable value types:

    int? x = null;
    int y = x ?? 5;        // y is 5

This will not work with strings because strings are not value types.
Jaime OlivaresSoftware ArchitectCommented:
well, start with this:
     string answer = (obj =! null)? obj: " ";
it can't be =!, should be !=:
     string answer = (obj != null)? obj: " ";

also, if obj is not string type, will produce a compiler error, but can be solved as:
     string answer = (obj != null) ? obj.ToString() : " ";
UnexplainedWaysAuthor Commented:
Yeah sorry it should lol, that's what happens when you quickly write up code in the box down the end of the page.

obj will be the same type as answer.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Jaime OlivaresSoftware ArchitectCommented:
so, my first post answers your question?
UnexplainedWaysAuthor Commented:
You fixed up the line that i used as an example of what i'm after, however i'm still looking for the shorter syntax.

what i'm after is something similar to this:  string answer = (obj : " ");
Jaime OlivaresSoftware ArchitectCommented:
no, there isn't shorter syntax than the ternary operator, assuming that 'obj' is string:
     string answer = (obj != null) ? obj : " ";

In C++ could be shorter, because null can be evaluate as a bool, something like:
    string answer = obj ? obj : " ";

But this is not possible with C#.
UnexplainedWaysAuthor Commented:
I've seen it before, it was in a blog and it was mentioned because no many people knew about it.

I know i wasn't dreaming.........
Jaime OlivaresSoftware ArchitectCommented:
maybe some compiler's unexpected behaviour, not a correct use at all...
UnexplainedWaysAuthor Commented:
I'm just anoyed i didn't save it.  I showed it to people @ work and no one else knew about it.
UnexplainedWaysAuthor Commented:

I just tested it out and it works.
            string a = null;
            string b = "something";
            string c = (a ?? b);

Here's more about it if someone reads this page later:

I knew it was something close  answer = (obj : " ");  >>>>>> answer = (obj ?? " ");
You're right, it does work on strings.

Ya learn something every day.
If you are reverring to c# shorthand then there are a couple of things i know...

1.  if ? else : then

eg,  bool AIsMoreThanTen =  (a > 10) ? true : false

the value before the question mark must equate to a bool, the value on the left of the colon is the 'then' and the right of the colon is the else and either the then or the else value is returned, the return values do not have to be a bool, only the value to the left of the question mark needs to be a bool


public string TestItem
return Session["TestItem"] as string ?? "Test Item"

The ?? operator checks if the value on the left is null, if it is, it returns the value on the right
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.