stuart-d2
10/8/2014 - 4:14 PM

Conditional (?:) : Negative & Positive Contexts

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("/"));
        
    }
}