# Example 5













 





 




using System;

class MainClass
{
  public static void Main ()
  {
    int class1 = 1;
    int class2 = 27;

    Console.Write("There ");
    Console.Write($"{(class1 > 1 ? "are" : "is")}");
    Console.Write($" {class1} ");
    Console.Write($"student{(class1 > 1 ? "s" : "")}");
    Console.WriteLine(" in Class1.");

    Console.Write("There ");
    Console.Write($"{(class2 > 1 ? "are" : "is")}");
    Console.Write($" {class2} ");
    Console.Write($"student{(class2 > 1 ? "s" : "")}");
    Console.WriteLine(" in Class2.");
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Notice that in this example, we used ternary operator ?: inside string interpolation expression. Unlike other expression, it is enclosed in brakcets ().

Also, in this example a single sentence is broken into 5 separate statements. This is done only to make the code easier to read for the students. Otherwise, it is not a requirement.

Last Updated: Apr 26, 2020, 5:13 AM