Even if you have used something for over a decade, you can learn about it :)
I was refactoring bits of code where someone clearly didn’t understand the benefits of enumerations, similar to this very contrived example:
using System;
namespace Demo
{
// explicit equivalence of:
// public enum TrafficLight { Red, Yellow, Green };
public enum TrafficLight { Red = 0, Yellow = 1, Green = 2 };
public class Program
{
public static void Main()
{
// old code:
Console.WriteLine(IntIsRedTrafficLight((int)TrafficLight.Red));
Console.WriteLine(IntIsRedTrafficLight((int)TrafficLight.Yellow));
Console.WriteLine(IntIsRedTrafficLight((int)TrafficLight.Green));
}
public static bool IntIsRedTrafficLight(int trafficLight)
{
return (trafficLight == (int)TrafficLight.Red);
}
}
}
The code was using way too many casts, and my goal was something as simple as this: Read the rest of this entry »





