Search code examples
c#resharperlanguage-featurescode-readability

What C# language features help you to reduce lines of code and improve readability?


I came across a C# language feature today courtesy of ReSharper, the ?? operator. This helped make the code even more concise than my initial attempt. See below for iteration in improving lines/length/readability of code.

A first attempt could be something like..

if (usersEmail == null)
  userName = firstName;
else
  userName = usersEmail;

Refactored to..

userName = usersEmail == null ? firstName : usersEmail;

Initially I thought the above would be the most efficient/concise version, but there is a third step...

userName = usersEmail ?? firstName;

Id like to know if you have any similar examples where C# language features help with reducing lines of code and improving readability?


Solution

  • This thread has a lot of gems: Hidden Features of C#? (including the one you mentioned)