Extension Methods (added to c# 3.0) have quite a few bad uses but a few positive uses have been found. One potentially use that I haven’t heard mentioned before is adding methods to Enums. As always, I’m sure there are dozens of ways to abuse this, and there may be better ways to do this but for some cases I think this might have some value. For example:
public enum ErrorStatus {
Open = 1, InProgress = 2, Closed = 3, WillNotFix = 4
}
public static class ErrorStatusExtensions {
public static string Color(this ErrorStatus status) {
if (status == ErrorStatus.Open) {
return "red";
}
else {
return "green";
}
}
}
Surely I’m not going to start adding dozens of methods to my enums and even more surely an not going to start storing data in opaque extension methods, but for a case like this, I think this just might work.