Sometimes you need to use “If Then Else” logic on something really simple, but all those brackets and parentheses are ugly and confusing. So instead use some fancy shorthand!
In this example I want to display the top 5 items in my array, but if my array is less than 5 items long I want to show all of the items up to the length of my array. (Otherwise I’d get an “index out of bounds” error for trying to access array items that don’t exist.)
int showTopEntries = 5; showTopEntries = showTopEntries >= totalEntries ? totalEntries : showTopEntries;
This reads as: If 5 is greater than or equal to my array’s length, then use my array’s length, else use 5.
Your significant other is gonna’ be really impressed with this bit of code! Well, unless your significant other is also a programmer… then my sarcasm is lost. :/

What if i dont want the else?
i.e.
showTopEntries = showTopEntries >= totalEntries ? totalEntries
is there a way to get that to work?
If you don’t want an ‘else’ in the statement, just set the ‘else’ portion to the value of the original variable. In that case, if something is true it gets set to a new value, otherwise it stays the same.
Example:
int a = 0;
int b = 10;
a = b > 100 ? b : a;
or
showTopEntries = showTopEntries >= totalEntries ?? totalEntries
thanks =)
It doesnt work in the followng scenario
showtopEntries = reader["showTopentries"] == DbNull.Value ? null : Convert.ToInt32(reader["showTopEntries"])
Shashi,
I typically do not reply to posts on any website whatsoever but your scenario may lead others to infer that the authors post is not a viable and effective option, where in fact it indeed is.
There are a couple problems with your posted scenario:
showtopEntries = reader["showTopentries"] == DbNull.Value ? null : Convert.ToInt32(reader["showTopEntries"])
1. I am assuming that you have declared showtopEntries as an Integer as you are converting one of the conditions to an Integer. Based on that assumption, you will not be able to implicitly assign a null value to an Integer as an Integer is a value type. One could assign null to a reference type such as a string. In C# a null value is the absence of anything and therefore not a value.
2. I realize that the author of this post has a typo within the declaration of the initial Integer, showTopeEntries where it should state showTopEntries. That said, C# is still a case sensitive language so if you mirrored the example above and remedied the typo, you will still need to ensure that the case is correct.
If you require an Integer as the result try the following scenario: int intShowTopEntries;
intShowTopEntries = reader["showTopEntries"] == DbNull.Value ? 0 : Convert.ToInt32(reader["showTopEntries"]);
If you require for example a string as the result try the following scenario: string ShowTopEntries;
ShowTopEntries = reader["showTopEntries"] == DbNull.Value ? null : reader["showTopEntries"].ToString();
Thanks for the clarification Tyler. I fixed the typo.
How can I write this shorter?
if (min > 0)
{
Data[2] = 0×00;
}
else {
Data[2] = 0×01;
}
I Tried
Data[2] = (min > 0) ? 0×00 : oxo1;
The Data array is Byte and min is integer. It should not be a problem. but in the short form it is not working.
Any ideas?
Im sure you found your answer already but for the next one who gets this problem:
Dont use the ( )
Just type:
Data[2] = (min > 0) ? 0×00 : oxo1;
Edit:
I mean it’s:
Data[2] = min > 0 ? 0×00 : oxo1;
The parenthesis doesn’t actually make any difference; it should still work with them on.
Sashi does have a point. In the following scenario the Thing1 property is nullable, and therefore does allow a null value. The tradition if/else works here but the shorthand version will give you an error.
public class Stuff
{
public Stuff( DataRow dr )
{
if( Convert.IsDBNull( dr["thing1"] ) )
{
Thing1 = null;
}
else
{
Thing1 = Convert.ToInt32( dr["thing1"] );
}
Thing2 = Convert.IsDBNull( dr["thing2"] ) ? null : dr["thing2"].ToString();
}
int? Thing1 { get; set; }
string Thing2 { get; set; }
}