Conditionals

if, else if, else

// if
if (arg) { };

// else if
if (arg) { } else if (arg2) { } else { };

for, do, do while

// for loop
(i = 0; i <10; i++);

int myLittleHorses = 0;

// while loop
while (myLittleHorses < 10)
{
    Console.WriteLine($"myLittleHorses = {myLittleHorses}");
    myLittleHorses++  // without incrementing here, this would run forever
}

// do while loop (will always run at least once)
do
{
    Console.WriteLine($"myLittleHorses = {myLittleHorses}");
    myLittleHorses++ 
} while (myLittleHorses < 0);

Switch Statements

    //...

Ternary Operator

(Slimmer if statement) Test & return first term if true, second if false

int bigger = x > y ? x : y;
// if x > y return x, else return y

Null Conditional

?. Used with collections & classes. "If null, return null, else return the value"

List<string> authors = null;
int? count = authors?.Count;
string messate = count == null ?
    "count is null" : "count is not null ";
Console.WriteLine(message);

Null Coalescing

?? "If the left hand operand is not null, return that. Else return the right hand operand."

    //...

results matching ""

    No results matching ""