Abstract Class versus Private Constructor

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;
}
 
P

Peter Rilling

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".
 
B

Bruno Jouhier [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top