C # - static class, private constructor, abstract class - all prevent instantiation - which one to use?

I am a little confused about using Static class, private constructor and abstract class

to prevent instantiation. (Confused about alternatives).

What is the scenario that works for each of them?

+2


source to share


4 answers


It depends on your needs.

  • A static class can be thought of as a "bunch of methods" - you would use it if you just need to group some methods, sample usage: MathHelpers, with methods like Sin, Cos, ConvertXToY (or host extension methods).

  • A private constructor is the one you would use when you want to be able to control how an object is created, for example, if you want to make sure that these objects can only be created by your static methods, Example:




class Robot
{
 public string Name { get; }
 private Robot() 
 { 
   // some code
 }

 public static Robot CreateAndInitRobot(string name)
 {
   Robot r = new Robot();
   r.Name = name;
   return r;
 }
}

      

  • Abstract classes. The ones you should use when you define any abstract object that shouldn't be initialized because it is incomplete / abstract and you want to further specialize it (by inheriting from it).
+3


source


You are using a static class when you only have static members. It doesn't work for any scenario where you want to inherit from a class.

A private constructor ensures that only code within the class itself can instantiate it, that is, a static method or an instance method on an already existing instance. Even if you inherit from a class, it still has to trigger the base class creation process, so it doesn't exist.



An abstract class is useless for preventing instantiation. You must inherit a class in order to use it for anything, and you can add any constructor you like to the descendant class.

+3


source


Static classes cannot be used as a base class for inheritance and can only contain static members.

Abstract classes cannot be instantiated at all and are only useful as a base class for inheritance. They can provide abstract memebers (no implementation) that override the derived class should , they can provide virtual members (with default implementation) that override the derived class, or they can provide regular (sealed) elements that provide general functionality.

Private constructors (usually found in non-abstract non-static classes) prevent the class from being instantiated without parameters / initialization. They are most useful when a class requires certain information to be in a valid state when initialized / built.

+2


source


An abstract class means that you must inherit from the class in order to have something to instantiate. It is usually used where the underlying functionality is generic, but where a certain specialization will be provided in a derived class that cannot be defaulted - in other words, you must provide your own behavior.

The private constructor was a pre.NET 2 way of preventing someone from instantiating a class using new - in .NET 2, a static constructor was introduced. This will be used wherever you want to have some kind of factory method, for example.

public static class MyClass
{
  static MyClass() {}
  private static MyClass LoadFromDatabase(int id)
  {
    // Get the data here....
  }
  public static MyClass LoadByKey(int id)
  {
    MyClass c = new MyClass();
    return c.LoadFromDatabase(id);
  }
}

      

+1


source







All Articles