Solved

Shorthand C#

Posted on 2007-11-25
12
2,373 Views
Last Modified: 2013-11-07
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 =)
0
Comment
Question by:UnexplainedWays
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20348303
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() : " ";
0
 
LVL 12

Author Comment

by:UnexplainedWays
ID: 20348360
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20348371
so, my first post answers your question?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 12

Author Comment

by:UnexplainedWays
ID: 20348406
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 : " ");
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20348429
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#.
0
 
LVL 12

Author Comment

by:UnexplainedWays
ID: 20348558
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.........
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20348607
maybe some compiler's unexpected behaviour, not a correct use at all...
0
 
LVL 12

Author Comment

by:UnexplainedWays
ID: 20348612
I'm just anoyed i didn't save it.  I showed it to people @ work and no one else knew about it.
0
 
LVL 7

Accepted Solution

by:
photowhiz earned 500 total points
ID: 20354779
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.
0
 
LVL 12

Author Comment

by:UnexplainedWays
ID: 20354824
BINGO!!!!!!!!!!!!!!!!!!!!!!!!

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: http://blog.benhall.me.uk/2007/10/c-null-coalescing-operator-and.html

I knew it was something close  answer = (obj : " ");  >>>>>> answer = (obj ?? " ");
0
 
LVL 7

Expert Comment

by:photowhiz
ID: 20360365
You're right, it does work on strings.

Ya learn something every day.
0
 

Expert Comment

by:Siconltd
ID: 26172622
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

2.  

public string TestItem
{
get
{
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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Security 2 27
Why does my excel data start at B1 instead of A1. 9 80
Cant save 3D 4 19
Problem!!! 8 33
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now