Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# if else shortcut

Tags:

c#

In C# how can I express the following if else statement using a shorter method(with ?):

 if (condition1 == true && count > 6)
           {
               dothismethod(value);

           }
           else if (condition2 == false)
           {

               dothismethod(value);
           }

My code looks really messy with these statements. Can someone direct me to a good resource on if then else short cut syntax?

like image 759
user1526912 Avatar asked Nov 21 '12 23:11

user1526912


2 Answers

It sounds like you're trying to write

if ((condition1 && count > 6) || !condition2)
    SomeMethod();
like image 139
SLaks Avatar answered Oct 03 '22 02:10

SLaks


? is not a "shortcut" if/else. It's called a ternary operator, and it's used when you want to assign a value to some variable based on a condition, like so:

string message = hasError ? "There's an error!" : "Everything seems fine...";

MSDN: http://msdn.microsoft.com/en-us/library/ty67wk28%28v=vs.100%29.aspx

like image 23
Tieson T. Avatar answered Oct 03 '22 02:10

Tieson T.