Conditional Operator Example
Posted
by mbcrump
on Geeks with Blogs
See other posts from Geeks with Blogs
or by mbcrump
Published on Tue, 23 Mar 2010 08:49:19 GMT
Indexed on
2010/03/23
16:03 UTC
Read the original article
Hit count: 609
Filed under:
If you haven’t taken the time to learn conditional operators, then now is the time. I’ve added a quick and dirty example for those on the forums.
Code Snippet
- using System;
- using System.Net.Mail;
- using System.Net;
- using System.Globalization;
- using System.Windows.Forms;
- class Demo
- {
- //Please use conditional statements in your code. See example below.
- public static void Main()
- {
- int dollars = 10;
- //Bad Coder Bad !!! Don't do this
- if (dollars == 1)
- {
- Console.WriteLine("Please deposit {0} dollar.", dollars);
- }
- else
- {
- Console.WriteLine("Please deposit {0} dollars.", dollars);
- }
- //Good Coder Good !!! Do this
- Console.WriteLine("Please deposit {0} dollar{1}.", dollars, dollars == 1 ? ' ' : 's');
- // expression ? true : false
- Console.ReadLine();
- }
- }
© Geeks with Blogs or respective owner