C# is a reliable and time-tested language. However, even the most experienced developers make mistakes from time to time. To write code that is easy to read and maintain, it’s important to follow best practices. This blog post will discuss some of th...

Markdown supported · ⌘↵ to post
Anthony Fung
Great tips in this article! Some additional notes I thought of:
(1) This is because Any() stops after checking for one element, whereas Count() will enumerate the collection.
(3) This is made easier when working with .NET 6 (and maybe some other versions) as the compiler will highlight possible unexpected nulls as warnings.
(5) Highly recommended, as this makes writing unit tests much easier too.
(6) Can be difficult sometimes. I feel that it’s ok to go above 3 for private methods. For a public API, think about combining the parameters into objects or structs where sensible. E.g. instead of having 3 parameters for x, y, and z co-ordinates, consider creating/using a Point to group them together.
(8) Highly recommended. Goes in line with Interface Segregation of SOLID principles.