Monday, 31 March 2008

Extension methods

C# 3.0 has introduced a new language feature called extensions methods. What extensions methods let you do is add new methods to any type without deriving from that type or modifying the source of that type. For example you can add a new method "StartsWithNum" to the type "String".


namespace ConsoleApplication1
{
public static class Foo
{
public static bool StartsWithNum(this string s)
{
for (int i = 0; i < 9; i++)
{
if (s.StartsWith(i.ToString()))
return true;
}
return false;
}
}
class Program
{
static void Main(string[] args)
{
string s = "hello";
Console.WriteLine(s.StartsWithNum());
}
}
}


Scott Gu's blog has a good post explaining extension methods.

In my opinion they are a bad idea because of the following reasons:
  1. Extension methods encourage that you to patch up your original code from files elsewhere.
  2. The programmer's "is-a", "has-a" dilemma gets an addition and gives us a trilemma: subclass, wrap or extend?
  3. Anyone not using Visual Studio 2008 is going to have a tough time navigating through the code as extension methods can be defined in any namespace.
  4. Programmers may start assuming that the extension method is part of the framework, when it may not be.

Sunday, 30 March 2008

Simsharp

Around an year back because of a certain amount of boredom and curiosity, I started writing an 8051 simulator in C#. I called it Simsharp, and managed to convice the guys at sourceforge that it was worthy of being hosted. I worked on it an hour a day for two months and then, when it was almost done, I stopped.

Few days back a friend of mine, who wanted to use an 8051 disassembler, asked me for my project. I couldn't give it to him because I had never done a release. The only other alternative was to ask him to check out the source from SVN and build it, which of course was tedious. So, I apologized and asked him to look elsewhere.

Today, after almost an year after I started, I put together a pre-alpha release. The source and binaries can be downloaded from the downloads page.

The simulator is hidden underneath a debugger. A debugger which itself runs with the help of the Boo interpreter.

There is support for the following in the debugger:

1. Loading of intel hex files
2. Disassembling the entire code.
3. Adding/Removing breakpoints.
4. Reading the internal RAM.
5. Reading the registers.
6. Single step
7. Run

I hope its of use to someone (other than the college students who will make this their course project).