Nullable types, works only with value types
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NullableTypes { class Program { static void Main(string[] args) { int? i = dr.GetIntFromDataBase() ?? 100; // using of ?? like if/else int? i = dr.GetIntFromDataBase(); if (i.HasValue) { Console.WriteLine("ok,value={0}",i.Value); } else { Console.WriteLine("null"); } } } class DataBaseReader { int? nullableInt = 10; // also like Nullable<int> someNullableInt = 10; double? nullableDouble = 3.14; bool? nullableBool = null; // string? s = "oops"; // << only for value types //methods public int? GetIntFromDataBase() { return nullableInt; } public double? GetDoubleFromDataBase() { return nullableInt; } } } |