• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 529
  • Last Modified:

c# if statement short cut using ?

I know there is a way to have short cut to reduce those {
can you show me how to convert them from the follow codes?

int inflatableValueInt;
                    if (inflatableValue != "")
                    {
                        inflatableValueInt = Convert.ToInt16(inflatableValue);
                    }
                    else
                    { inflatableValueInt = 0; }
0
solution1368
Asked:
solution1368
  • 5
  • 5
  • 2
1 Solution
 
p_davisCommented:
!Sting.IsNullOrEmpty(inflatableValue) ?  inflatableValueInt = Convert.ToInt16(inflatableValue):inflatableValueInt = 0;
0
 
p_davisCommented:
warning though.... you are converting to a short instead of an int32
0
 
anarki_jimbelCommented:
I'd recommend to read about this "?" ternary operator:
http://msdn.microsoft.com/en-us/library/ty67wk28.aspx
http://www.dotnetperls.com/ternary

As you can see, "...At compile time, the C# compiler translates the ternary expression into branch statements ...". In other words, basically the IL code is same for "?" and if else. In other words, shorter code does not make your program to run faster.

Also, when you have just one line in your if statements you may write like below, withour curly braces:

            if (inflatableValue != "")
                inflatableValueInt = Convert.ToInt16(inflatableValue);
            else
                inflatableValueInt = 0; 

Open in new window


Honestly, I don't like this approach and prefer the form with braces. The reason is that if you need to modify your code and add some lines, and forget to add braces you are in trouble as code will not compile or give wrong results etc.
So, nothing really wrong with your original code :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
p_davisCommented:
I believe it is a matter of preference in regards to what you think is more readable. Which is why i thought the question wasasked
0
 
anarki_jimbelCommented:
Indeed!

I commented just in case. I've seen people thinking that shorter code will execute faster (they even removed all blank lines :)).
0
 
käµfm³d 👽Commented:
Why not use TryParse?

int inflatableValueInt = 0;

int.TryParse(inflatableValue, out inflatableValueInt);

Open in new window


TryParse returns a boolean, but it's not set in stone that you must inspect the return value. If the conversion succeeds, then inflatableValueInt will hold the converted value; otherwise, it will have the default of 0.
0
 
anarki_jimbelCommented:
I agree - this may work. However in some cases it may be not appropriate. For example, "10" should be converted to an integer, "" should be converted to 0 (again - integer), but "abc" - should raise an error because not-numeric values should not appear in this field...
0
 
käµfm³d 👽Commented:
should raise an error because not-numeric values should not appear in this field...
Then just switch to Parse, or just just use the Convert.ToInt16 (or 32) call by itself. As it stands, given "abc" an exception going to be rasied anyway  = )
0
 
p_davisCommented:
I see, so giving the answer you asked for, doesn't get awarded points. How novel.
0
 
anarki_jimbelCommented:
"I see, so giving the answer you asked for, doesn't get awarded points. How novel." [p_davis]

I don't mind to share points with p_davis.
Hope he has nothing against my solution to get rid of curly braces  :)
0
 
p_davisCommented:
not at all anarki. just upsetting when you spend time and knowledge to no effect. However, I got over it very quickly =).
0
 
anarki_jimbelCommented:
I have a feeling that not everyone at EE knows that points can be split, and this is a bit upsetting, I agree. Quite often there are more than one valid answer. Good luck! :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now