.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

The Ornery Ternary Operator

Posted By:Shashi Ray       Posted Date: February 22, 2009    Points: 10    Category: C#    URL: http://www.dotnetspark.com  

The Ornery Ternary Operator


C# has unfortunately inherited one of C/C++ and Java's most opaque constructions, the ternary operator. The statement:

if ( a > b )

z = a;


z = b;

can be written extremely compactly as:

z = (a > b) ? a : b;

The reason for the original introduction of this statement into the C language was, like the post- increment operators, to give hints to the compiler to allow it to produce more efficient code, and to reduce typing when terminals were very slow. Today, modern compilers produce identical code for both forms given above, and the necessity for this turgidity is long gone. Some C programmers coming to C# find this an "elegant" abbreviation, but we don't agree and will not be using it in this book.


Shashi Ray

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend