 <p>In addition to <em>Rene Vogt</em>'s answer, you can use <code>switch</code> statement combined with null conditional operator. It makes code more readable: </p> <pre><code>switch (var?.ToString()) { case "A": return 1; case "B": return 2; default: return 0; } </code></pre>