condition ? consequent : alternative var rand = new Random(); var condition = rand.NextDouble() > 0.5; var x = condition ? 12 : (int?)null; https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator