Abstract Class versus Private Constructor

  • Thread starter Thread starter Andrew Robinson
  • Start date Start date
A

Andrew Robinson

I need to create a shared static field for use within a number of different
classes. Which one should I be using or are they all really the same thing?

public class Widget
{
private Widget() {}
public static string DataField = string.Empty;
}

versus

public abstract class Widget
{
public static string DataField = string.Empty;
}

versus

public abstract class Widget
{
private Widget() {}
public static string DataField = string.Empty;
}
 
Do you expext this class to ever be instantiated? Is this just considered a
utility class? Normally for utility classes, I make then "sealed" and use a
"private" constructor".
 
The private constructor is the starndard trick. Whether you make the class
abstract or not (or even sealed) won't matter much.

In the next version (VS 2005), you can apply the static keyword at class
level. Then, you don't need this private constructor trick any more.

Bruno.
 
Back
Top