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?
It sounds like you're trying to write
if ((condition1 && count > 6) || !condition2)
SomeMethod();
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With