Web Dev Matters and Me

Web Development Matters - HTML, XML, C#, .NET, AJAX/Javascript(jQuery), CSS, XML-XSLT

ME - LIFE,Philippines, Tokyo, ECE, PhilNITS/JITSE,情報処理, 日本語

things about Philippines, gaming, C# development and web development, how to make money in stock trading

Web Dev Matters and Me

The questionable Question Mark (?) in C#

This under-rated character can make writing C#(or Perl) codes easier and simple.

It can either be a comparator or a null-conditional if-else statement, in just a single line.

it can simplify 

string SampleText;

if(Datetime.Now.Month == 12)
{
SampleText = "Christmas Month";
}
else
{
SampleText = "Some Other Month";
}


into

string SampleText = Datetime.Now.Month == 12 ? "Christmas Month" : "Some Other Month";


or much even this

string SampleText;

if(passedParameter != null)
{
SampleText = passedParameter;
}
else if (passedParameter1 != null)
{
SampleText = passedParameter1;
}
else if (passedParameter2 != null)
{
SampleText = passedParameter2;
}
else if (passedParameter3 != null)
{
SampleText = passedParameter3;
}
else
{
SampleText = string.Empty;
}
to simple



string SampleText = passedParameter ?? passedParameter1 ?? passedParameter2 ?? passedParameter3 ?? String.Empty;


and this is not limited to strings, it also applies to Objects.


More to share as we go to the world of coding (^-^)

FB Connect