Tip of the day: Using the null-coalescing operator over the conditional operator

I’ve recently been refactoring a lot of code that used the conditional operator and looked something like this: int someValue = myEntity.SomeNullableValue.HasValue ? myEntity.SomeNullableValue.Value : 0; That might seem less verbose than the traditional alternative, which looks like this: int someValue = 0; if (myEntity.SomeNullableValue.HasValue) someValue = myEntity.SomeNullableValue.Value; …or other variations on that theme. However, […]

Why should you be returning an IEnumerable

I’ve seen in many places where a method returns a List<T> (or IList<T>) when it appears that it may not actually really be required, or even desirable when all things are considered. A List is mutable, you can change the state of the List. You can add things to the List, you can remove things […]