Skip to content

5.5 Exercise 5.3 Nullable and value types

  1. Add the following code at the end of your Main method:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    int integer1 = 3;
    int integer2 = 3;
    int? integer3 = 3;
    int? integer4 = 3;
    
    Console.WriteLine(integer1);
    Console.WriteLine(integer2);
    Console.WriteLine(integer3);
    Console.WriteLine(integer4);
    
    • The first line assigns the value 3 to a variable of the primitive type.
    • The second line also uses a primitive type.
    • The third and fourth lines create nullable integers. The ? makes the value type nullable.
    • Note that int and int? can both be printed with Console.WriteLine; the compiler handles the conversion.
    • int? can hold null, which primitive int cannot.
  2. Run your program and note that the value 3 is displayed four times.