Thursday, April 3, 2008

C# quirk for the day

Interfaces in the .Net world are always prefixed with an "I" for example if I create an interface "StuffDoer" I would create an C# interface called "IStuffDoer".

Now although I've never needed to employ this practice I don't have a problem with it, it is also not uncommon in the Java world either, Tapestry for example seems to like the "I" prefix convention on interfaces.

.Net has however made it part of it's coding standard, and once you start working with C# it quickly becomes apparent why this is so.

C# uses the same ":" operator for inheritance as it does with interface implementation. The C# compiler will always assume the first class after the ":" is a class being extended, and anything after that will be an interface.

For example:


public class MyObject : Object, IDisposable



The compiler can figure out that Object is a class and IDisposable is an interface.

But what about the poor programmer?

Lets drop the I convention for a second and let "IDisposable" become "Disposable":


public class MyObject : Object, Disposable



You as a programmer have no way of determining if Object is indeed an interface or a class just by looking at the code, you have no choice but to go look at the definition.

No comments: