A
Andrew Ducker
Static methods are fantastic. Static classes even moreso. You can
call them from anywhere in a very simple manner, making them useful
"Helper" classes. But they lack one very useful trick - they can't be
subclassed. If you want a range of helper classes with similar
functionality then you either have to put that shared code into a
different static class that they all access or use a singleton
pattern instead.
(And while I don't know how often the singleton pattern is used
elsewhere, it's certainly one that I bump into on a very regular
basis. For those that don't about it, it's a class that only allows a
single copy of itself to be instantiated. For more about them, and
guidelines on writing your own see http://www.yoda.arachsys.com/csharp/singleton.html
where Jon Skeet gives some useful examples.)
In any case, it's something that's common, too easy to implement badly
in multithreaded applications, and the resulting code isn't as pretty
as it could be. What I'd like is a new keyword in the language that
solves all of these - "singleton".
If a class is declared "singleton" then it should be possible to
access its methods directly, in the same way that a method is on a
static class. In effect, MySingleton.Instance.MyMethod() would be
replaced by MySingleton.MyMethod() - and the compiler would deal with
creating your singleton in a safe and efficient way.
Code before:
public class SingletonClass
{
private SingletonClass(){ }
public static readonly SingletonClass Instance = new
SingletonClass();
public void MyMethod()
{
....
}
}
and to call it:
SingletonClass.Instance.MyMethod();
Code after:
public singleton class SingletonClass
{
public void MyMethod();
}
and to call it:
SingletonClass.MyMethod();
And yes, I know the chances of me actually getting a change made to C#
is slim to none, but this has been bugging me for a few days, and I
thought I'd throw it out there to see what other people thought.
Cheers,
Andy
call them from anywhere in a very simple manner, making them useful
"Helper" classes. But they lack one very useful trick - they can't be
subclassed. If you want a range of helper classes with similar
functionality then you either have to put that shared code into a
different static class that they all access or use a singleton
pattern instead.
(And while I don't know how often the singleton pattern is used
elsewhere, it's certainly one that I bump into on a very regular
basis. For those that don't about it, it's a class that only allows a
single copy of itself to be instantiated. For more about them, and
guidelines on writing your own see http://www.yoda.arachsys.com/csharp/singleton.html
where Jon Skeet gives some useful examples.)
In any case, it's something that's common, too easy to implement badly
in multithreaded applications, and the resulting code isn't as pretty
as it could be. What I'd like is a new keyword in the language that
solves all of these - "singleton".
If a class is declared "singleton" then it should be possible to
access its methods directly, in the same way that a method is on a
static class. In effect, MySingleton.Instance.MyMethod() would be
replaced by MySingleton.MyMethod() - and the compiler would deal with
creating your singleton in a safe and efficient way.
Code before:
public class SingletonClass
{
private SingletonClass(){ }
public static readonly SingletonClass Instance = new
SingletonClass();
public void MyMethod()
{
....
}
}
and to call it:
SingletonClass.Instance.MyMethod();
Code after:
public singleton class SingletonClass
{
public void MyMethod();
}
and to call it:
SingletonClass.MyMethod();
And yes, I know the chances of me actually getting a change made to C#
is slim to none, but this has been bugging me for a few days, and I
thought I'd throw it out there to see what other people thought.
Cheers,
Andy