A static class can never be created. No way, no.
A non-static class with a private constructor, but all static methods can be used in various ways - inheritance, reflection, calling a private constructor in a static factory - to create an instance of the class.
If you never want to instantiate, I would go with a static class.
Edit - Refinement for FosterZ comment
Say you have this utility class:
public class Utility { public static string Config1 { get { return "Fourty Two"; } } public static int Negate(int x) { return -x; } private Utility() { } }
If another developer is not aware of his intention, he can do this:
public class Utility { public static string Config1 { get { return "Fourty Two"; } } public int Config2 { get; set; } public static int Negate(int x) { return -x; } private Utility() { }
You now have a Frankenstein class. Some of its functions require instantiation, and some do not. Maybe this is what you want, but maybe not. You can prevent this from looking at the code, but why not make your intentions explicit in the code? Marking the class as static eliminates any possible confusion. You cannot create a static class or inherit it.
Corbin March Nov 27 '08 at 5:55 2008-11-27 05:55
source share