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 (^-^)
0 comments:
Post a Comment