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

Top 5 Contributors of the Month
Melody Anderson

Home >> Code Snippets >> DateTime >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Ternary Operator in C#.net

Posted By:Narayanan       Posted Date: April 12, 2013    Points: 40    Category: DateTime    URL: http://www.dotnetspark.com  

Today we will discuss about how to use Ternary Operator in C#net with Example.

Ternary Operator:  It is a One line solutuon for Compare two values

Syntax for Ternary Operator

?:, the ternary conditional expression
condition ? value_if_true : value_if_false

How to use Ternary Operator in C#.net?
For Eg:
Display dateTime value in DateTime Object .if input value is null ,object should be display null.

  Nullable<DateTime> reg_date = null;
  reg_date = textBox1.Text == string.Empty ? null : (DateTime?)(Convert.ToDateTime(textBox1.Text)); 

Here, we declare DateTime object with null value and then we used Tenary Operator.
In Left Side call declare Object name
In Right Side: Give a Condition, input box is empty ,object value is displayed null value.


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

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