Thursday, February 28, 2008

C# Operator ?

Instead of operator 'if' sometimes we can use operator '?'

code with operator 'if'

        string getName(string name)
{
if (name == null)
return "default";
else
return name;
}


code with operator '?'

        string getName_with_operator_question(string name)
{
return (name == null) ? "default" : name;
}

Additional Links:

?: Operator (C# Reference)

No comments: