Skip to main content

C# ?: (Question Mark Dot) Operator

I liked this operator. It is called "ternary" operator too.
I can use it instead of basic "if" statements.

Syntax of this:


condition ? consequent : alternative
And the code sample:

 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using System;
class Operators
{
    static void Main()
    {start: //a label to start again
        Console.Write("Do you want to continue? (y, n): ");
        string answer = Console.ReadLine();
        Console.WriteLine(answer == "y" ? "Congrats!" : "See you later!");
        goto start;
    }
}

Comments