Code Review: Making a drop down list out of an enum

I’ve come across code like this a couple of times and it is rather odd: IEnumerable<CalendarViewEnum> _calendarViewEnums = Enum.GetValues(typeof(CalendarViewEnum)).Cast<CalendarViewEnum>(); List selectList = new List<SelectListItem>(); foreach (CalendarViewEnum calendarViewEnum in _calendarViewEnums) { switch (calendarViewEnum) { case CalendarViewEnum.FittingRoom: selectList.Add(new SelectListItem { Text = AdminPreferencesRes.Label_CalendarViewFittingRoom, Value = ((int)calendarViewEnum).ToString(CultureInfo.InvariantCulture), Selected = (calendarViewEnum == CalendarViewEnum.FittingRoom) }); break; case CalendarViewEnum.Staff: selectList.Add(new SelectListItem […]

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 […]

Tip of the Day #15: Loop Performance

When you look at the code it will probably seem somewhat obvious, but it is interesting how the same thought process isn?t necessarily there when actually developing the code, especially when under the pressure of a looming deadline. Take for example this snippet of code (from a fictitious hotel management system) that may have been […]