foreach (string text in new[] { "3", "0", "x" }) Console.WriteLine(Describe(Parse(text))); Console.WriteLine(Describe(default)); static Outcome Parse(string text) => int.TryParse(text, out int quantity) && quantity > 0 ? new Accepted(quantity) : new Rejected($"rejected:{text}"); static string Describe(Outcome outcome) => outcome switch { Accepted accepted => $"ok:{accepted.Quantity}", Rejected rejected => rejected.Reason, null => "empty", };