March 4, 2008

SD West, Day 1

Events
I thought I'd jot down a few notes of some of the cooler things I see while at SD West.

C# 3.0 Method Extensions

File as: Better code organization. In C# 2.0 you might do something like this: public static class MyStringUtils { public static string PrependDate(string str, DateTime date) { string strOut = date.ToShortDateString(); strOut += ": "; strOut += str; return strOut; } } // And use it: string s = "Try this."; string s2 = MyStringUtils.PrependDate(s, DateTime.Now); It works, but you end up with little utility classes when you want to do something specific with a string that the developers of the string class didn't think of or didn't need. All developers need to know where your special class is and to use it when they want to do this action. C# 3.0 lets you make a method an extension on some other class. The above example would be done like this: public static class MyStringUtils { public static string PrependDate(this string str, DateTime date) { string strOut = date.ToShortDateString(); strOut += ": "; strOut += str; return strOut; } } // And use it: string s = "Try this."; string s2 = s.PrependDate(DateTime.Now); The code differences are relatively small, but the implications are large. Essentially we're saying we wish this built-in class had this method, and now we have a way to do that without dealing with knowing to use a specialty side class or creating a new inherited string class. They even conveniently are available in Intellisense:

C# 3.0 Lambda Expressions

File as: Shortcuts to easy, readable code. I thought foreach was a great thing in C#: foreach (string str in strList) { Console.WriteLine(str); } It at least eliminated that nastiness of a temporary int variable that counted up through the string list until it hit the count of number of strings in the list. C# 2.0 had anonymous delegates to make it a little shorter: strList.ForEach(delegate(string str) { Console.WriteLine(str); });but the syntax is a bit confusing, and therefore rarely used. C# 3.0 makes it short and neat: strList.ForEach((str) => Console.WriteLine(str)); There were many other things in C# 3.0 that I found useful, but these were my top two picks.
{democracy:4}