This year I've decided to delve into C#. I haven't touched Visual Studio since before .NET. I don't know anything about the framework, the languages, or windows programming. Aiming to rectify, I picked up C# in Depth and commenced skimming!
Now, I've made my fair share of M$ snide asides, but I'm having a hard time coming to gripes with C#. Everything I run into either "just works" or exceeds my expectations. And the cool features are in fact, quite cool! Color me impressed!

Noob!
Being a TOTAL noob, the first thing I did was rewrite a few of my
Project Euler Solutions to buff up on the syntax. After I got the semi-colons and brackets all figured out, I moved on to something a little bigger.
I wanted a simple program to run and benchmark my solutions, so I wouldn't have to do as much leg work every time I converted a problem. I figured this would be a simple enough thing to do, and it would provide a good foundation for a future gui application and beginning unit testing.
I wanted to share some particulars that I thought were pretty cool, you can grab the code I'm talking about from the google code repository, and follow along...or something.
Generics, Delegates and Lambdas
Generic Collections provide a data structure that I can access and use just like an array, but also provides methods for dealing with delegates.
Delegates are very similar to closures, blocks, procs, and lambdas like I've worked with in other languages, so the transition was smooth. The lambda syntax was particularly reminiscent of pythonic list comprehensions.
Thanks to delegates, I can turn this:
var matchingTypes = new List<Type>();
foreach(t in CurrentTypes) {
if(t.IsSubclassOf(parentType) {
matchingTypes.Add(t);
}
}
return matchingTypes;
Into this:
return CurrentTypes.FindAll(
delegate(Type t)
{
return t.IsSubclassOf(parentType);
}
);
And finally, via lambda, to this!
return CurrentTypes.FindAll(
t => t.IsSubclassOf(parentType)
);
Not too shabby, eh?
Reflection
Most of my programming has been in ColdFusion, JavaScript and Ruby. There's been a little bit of this and a little bit of that peppered in there, particually C and Java while I was at UCF, but for the most part I've enjoyed working with dynamic and/or interpreted languages. Meta-programming is common in these types of languages, but I was surprised and impressed to read up on reflection. In this case, reflection allows me to dynamically detect and run my problems, which makes it easier (and cleaner) for me to add new solutions.
Here's a simplified "ClassMaster" class I use to wrap my reflection calls for listing and creating classes, so you can see what I'm on about:
class ClassMaster
{
private Assembly CurrentAssembly { get; set; }
private List<Type> CurrentTypes { get; set; }
public ClassMaster()
{
CurrentAssembly = Assembly.GetExecutingAssembly();
CurrentTypes = new List<Type>(CurrentAssembly.GetTypes());
}
// should probably take arguments to pass thru...somehow
public Object CreateClass(Type classType)
{
return CurrentAssembly.CreateInstance(classType.FullName);
}
public List<Type> getTypesByParentClass(Type parentType)
{
return CurrentTypes.FindAll(
t => t.IsSubclassOf(parentType)
);
}
}
That's it for now. I'll be looking into LINQ and unit testing in the next couple weeks, and then I'm on to the gui. ASP, SilverLight, and WPF here I come!
Here are those links again:
Release
Latest Version
Tags: c#, meta-programming, project euler
Okay, so it's hardly an announcement, but I've been working on a flex app for determining your daily official xkcd meetup location via the geohashing method described in comic 426.
Much love to:
I'm still learning Flex and this is my first adventure with Google Map, so it's rough. And there's still a lot of work to do, like, validation, error checking, or testing for example. But hey, first pass!
Here's the code for the actual GeoHash class. I have "view source" disabled because it currently contains my API key, but you can download the key-less zip.
public class GeoHasher {
private var date:Date;
private var dow:Number;
private var latitude:Number;
private var longitude:Number;
private var dateHash:String;
private var dowHash:String;
private var ready:Boolean;
public function GeoHasher() {
this.ready = false;
}
public function reset(
date:Date,
dow:Number,
latitude:Number,
longitude:Number
):void {
this.date = date;
this.dow = dow;
this.latitude = latitude;
this.longitude = longitude;
this.ready = true;
setHashValues();
}
public function getDestination():Object {
if(ready) {
var hash:Object = getHashAsDecimal();
return {
latitude: toOrdinal(latitude, hash.date),
longitude: toOrdinal(longitude, hash.dow)
};
}
return { latitude: "", longitude: "" };
}
// privates
private function setHashValues():void {
var formattedDate:String = Utilities.dateFormat(date,"-");
var string:String = formattedDate + "-" + dow.toString();
var hash:String = MD5.encrypt(string);
dateHash = hash.slice(0,hash.length / 2);
dowHash = hash.slice(hash.length / 2,hash.length);
}
private function toOrdinal(pre:Number, post:Number):String {
return (Math.floor(pre) + post).toString();
}
private function getHash():Object {
return { date: dateHash, dow: dowHash };
}
private function getHashAsDecimal():Object {
var hash:Object = getHash();
return {
date: Utilities.fractionalHexToDecimal(hash.date),
dow: Utilities.fractionalHexToDecimal(hash.dow)
};
}
}
Tags: actionscript, flex, geohashflash, geohashing, xkcd
I've always liked the month of June. It's my birth month, but it's also close enough to the middle of the year, which makes it a great time to re-evaluate my New Years Resolutions.
I've done pretty terrible on that list. However, I've made a lot of progress in areas I hadn't anticipated 6 months ago.
All in all, it's been a pretty good year and things are looking up!
Tags: chatter, resolutions
I made a few user interface updates to my crappy Flex version of Conway's Game of Life.
The board now wraps, and I added a Timer with an adjustable delay via the lovely HSlider control. I wanted to replace the cells with Sprites but it's giving me grief and I want to play The Sims.
I'm still making my mind up about Flex, but I have to admit that aside from the Sprite issue I've been pleasantly surprised at how easy everything's been coming together.
You can view source on the application, or you can download the zip.
Tags: actionscript, flex, game of life