Conditional (?:) : Negative & Positive Contexts
//linqpad
// SYN : condition ? first_expression : second_expression;
// condition returns true or false i.e. ? true : false
void Main()
{
string path = "/";
string pathNegative = "";
string pathPositive = "";
pathPositive = path.StartsWith("/") ? "/" + path : path;
pathNegative = !path.StartsWith("/") ? "/" + path : path;
Console.WriteLine(pathNegative);
Console.WriteLine(pathPositive);
/*OUTPUT :
/
//
*/
/* Alternative Method - Main() sending argument to method
Url() and getting a quick return... */
class ConditionalOp
{
static string Url(string path)
{
return path = path.StartsWith("/") ? "/" + path : path;
}
static void Main()
{
Console.WriteLine(Url("/"));
}
}