Some very smart people have balked at the idea of prefixing C# interfaces with the letter “I". While I dislike Hungarian notation, I am in favor of IInterfaces.
My main reason is the C# language itself. Consider this code:
public class Employee : Person
Is Person an interface or a class? Proponents of the no-I convention might say that it shouldn’t matter. In C#, though, it does matter. In a single-inheritance language, you only get one base class. Also, if I want to see all of the behavior for Employee, I may want to look at the code for Person as well if it’s a class.
Java has the implements and extends keywords; C# only has the colon (:). In Java you can identify interfaces at a glance; in C# you can’t.
My other reason is that it’s the standard. Microsoft’s conventions say to prefix interface names with the letter I. Most of the C# code I’ve seen does. Deviating from this convention can confuse developers.
I hope we can see eye to I on this.
I have upgraded the Singleton Pattern from Evil to Mostly Evil. Last week I actually found a valid use case for it.
I had a class that used the Strategy Pattern; a constructor parameter was an interface with a method for the algorithm. An IoC Container would have been overkill here; this was in a library module, and the class was only used by one other class in the library. That instantiated it twice, once for each of two algorithms (two strategies, each implementing the interface). The calling class had to know which algorithm to use each time.
The algorithms were pretty simple, and had no state. They were a little too complex for a lambda, though.
So I used Lazy<T> to make the strategies/algorithms Singletons, giving each a static Instance property that returned a lazy-created instance. (As an aside, if you make the constructor private you have to specify a lambda/delegate to Lazy or you’ll get a runtime exception.) I sealed the strategies to be safe.
If this seems like a very narrow use case to you, it does to me, too. But, if the stars align, you may be able to use the Singleton Pattern safely too. Hopefully without summoning the Great Old Ones*.
* - Some say Azathoth is itself a Singleton…
Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.
Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.