开发者

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

开发者 https://www.devze.com 2023-01-10 05:05 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 12 years ago.

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 = usersEm开发者_开发问答ail ?? firstName;

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


the using block, LINQ, anonymous delegates, the list would just go on..

C# has a very nice habit of introducing features in every major release that cut down the amount of code that you have to write.


The var keyword for implicit static typing and automatic properties are two good examples.


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


Using using keyword


Extension methods.


LINQ queries allowing you to express the query criteria better than a foreach loop

0

精彩评论

暂无评论...
验证码 换一张
取 消