Static Class vs Protected Constructor - c#

I Am getting a warning message in my class, like
Add a Protected constructor or the static keyword to the class declaration
Solution
The error is gone after I tried both the below ways,
static class without constructor
public static class Program {
}
Non static class with protected using constructor
public class Program
{
protected Program() { }
}
Question:
So What is the difference between Static Class vs Protected Constructor which is mentioned in my above solution? And which one is best to use?

A static class doesn't need an instance to access its members. A static class cannot have instance members (e.g. public int MyNumber; is not allowed on a static class because only static members are allowed on a static class). Both instance and static members are allowed on a non-static class though. A class with a protected constructor can only have an instance created by itself or something that inherits from it.
public class Program
{
protected Program()
{
// Do something.
}
public static Program Create()
{
// 100% Allowed.
return new Program();
}
public void DoSomething()
{
}
}
public static class AnotherClass
{
public static Program CreateProgram()
{
// Not allowed since Program's constructor is protected.
return new Program();
}
}
public class SubProgram : Program
{
protected SubProgram()
{
// Calls Program() then SubProgram().
}
public new static Program Create()
{
// return new Program(); // We would need to move the SubProgram class INSIDE the Program class in order for this line to work.
return new SubProgram();
}
}
Program.Create(); // Can be called since Create is public and static function.
Program.DoSomething() // Can't be called because an instance has not been instantiated.
var test = Program.Create();
test.DoSomething(); // Can be called since there is now an instance of Program (i.e. 'test').
AnotherClass.CreateProgram(); // Can't be called since Program's constructor is protected.
SubProgram.Create(); // Can be called since SubProgram inherits from Program.
As for performance, this distinction doesn't really have much to do with performance.

You probably only have static members in the class and the code analyser assumes that your intention is to not be able to create instances of the class so it is asking you to either make the class static
public static class Program {
//...static members
}
or put a protected/private constructor
public class Program {
protected Program { //OR private
}
//...static members
}
to prevent instances of that class from being initialized.
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
Reference Static Classes and Static Class Members (C# Programming Guide)
The protected constructor means that only derived classes can call the constructor
and a private constructor wont allow any other classes to initialize the class with a private constructor

A static constructor is called when the class type is instantiated. The protected constructor is called when an instance of a class is created. The protected part means only classes that inherit the class can call it.

Static Constructor: Called once when the class type is instantiated and is used to initialize static members. Does not create an instance of the class.
Protected Constructor: A constructor that can be called only by the class or a class that inherits it.
The best practices for this is that you should have a static constructor for initializing static members and a protected constructor if you only want classes that inherit to be able to create an instance of your class. You can have both.
public class MyClass
{
static readonly long _someStaticMember;
private bool _param;
static MyClass()
{
//Do Some Logic
_someStaticMember = SomeValueCalculated;
}
protected MyClass(bool param)
{
_param = param;
}
}
public class ChildClass: MyClass
{
public ChildClass(bool param) : base(param);
}
public class NotChildClass
{
public MyClass someObject = new MyClass(true); //Will Fail
}

Related

Why objects of the static class cannot be created? and which constructor a static class contains?

As i searched and got few points static class is sealed and uses private constructor internally then why in my code i am not able to access the method using the class name using System?
using System;
public sealed class ClasswithPrivateCons {
private ClasswithPrivateCons() { }
public void Printname() { Console.WriteLine("Hello world"); }
}
public class Program {
public static void Main() {
ClasswithPrivateCons.Printname(); // gives error
}
}
It would be useful if you also included the error you're getting, but in this case it's obvious: Printname is not a static method, so you can't call it without an instance of ClasswithPrivateCons(). Since that constructor is private, you can't construct those objects.
Three resolution options:
Make Printname() static; then ClasswithPrivateCons.Printname() will work
Make the constructor public; then new ClasswithNoLongerPrivateCons().Printname() will work
Add a public static factory method that calls the private constructor:
public static ClasswithPrivateCons New() { return new ClassWithPrivateCons(); }
ClassWithPrivateCons.New().Printname()
Sealed class is not static. When a class is defined as a sealed class, this just means that class cannot be inherited. You must create instance of it before use. Sealed class with private constructor doesn't automatically become a static class. Static class has behaviour like sealed, but it represents separate kind of object in OOP. "Sealed" just means that class cannot be inherited, but in any case we must create an instance. At the same time, static class can't be instantiated.
Constructor of class must be public, otherwise we could not create instance. Instance constructor not only initialize new object, it also responsible for creating new objects and getting them ready to use. Constructor, unlike ordinary method, leverages inner .NET mechanisms aimed to create new instance.
public sealed class ClasswithPrivateCons
{
public ClasswithPrivateCons() { }
public void Printname() { Console.WriteLine("Hello world"); }
}
class Program
{
static void Main(string[] args)
{
ClasswithPrivateCons cl = new ClasswithPrivateCons();
cl.Printname();
}
}
Also Printname method can be declared as static. Than it may be called
ClasswithPrivateCons.Printname();

C#: Protected Variables inside of a Generic Class can be accessed by a different subclass of that Generic Class. Can I prevent this?

Say I have a generic class Foo, that has a variable that is protected
public class Foo<T>
{
protected bool knowsFu;
}
I also have 2 sub-classes: Bar and Pipe
public class Bar : Foo<Bar> {}
public class Pipe : Foo<Pipe> {}
It is actually possible for me to access the knowsFu in Pipe FROM Bar, e.g.:
public class Bar : Foo<Bar>
{
void UpdateFuInOtherClass(Pipe p)
{
p.knowsFu = false;
}
}
Is this intended behaviour? (If so, what would be the usecase?)
Is there a way for me to prevent other Foo-Subclasses from modifying/reaching the protected variable inside of my current subclass?
More specifically: I'm using a generic class to implement the Singleton-Pattern:
https://en.wikipedia.org/wiki/Singleton_pattern
However, I'm currently able to access any singleton's protected instance-variable, as long as I am inside of another Singleton. Is there a way to prevent this?
EDIT: It might be relevant to note that the protected variable (knowsFu) is actually STATIC as well.
EDIT2: Ok, maybe the example was abit too generic.. here's how I'm actually currently implementing it:
why use Singleton? A:The platform I'm working on is Unity3D, in which the pattern is used frequently
I have a generically typed abstract class SingletonBehaviour
public abstract class SingletonBehaviour<T> where T : MonoBehaviour
{
public static T Instance { get { return instance; } }
protected static T instance { get; private set; } }
// Loading is done through Unitys Awake-Method
}
One of the Singleton-Objects that I'm using is the APIManager
public class APIManager : SingletonBehaviour<APIManager>
{
// Methods like SendHTTPPost(), HTTPGet(), etc.
}
However, since most of my projects need some better API-implementation than that, what I'm currently doing is:
public class ProjectAAPIManager : APIManager
{
// Overriding Instance so my return value is not APIManager but instead ProjectAAPIManager
public static new ProjectAAPIMamager Instance { get { return (ProjectAAPIManager)instance; } }
}
This ^ is the reason my (inner) instance-variable is protected, and not private.
However, because of this, any other SingletonBehaviour in my project can now access the (inner) instance-variable on my ProjectAAPIManager
public class GameController : SingletonBehaviour<GameController>
{
private void AMethod()
{
// Accessing inner variable instead of public one
ProjectAAPIManager.instance.DoSomething();
}
}
As it's only the getter, this currently does not really matter. But what if I'd need access to the setter in my subclass as well?
Also: would it be worth it to generically type my APIManager as well?
Your question is nothing short of bewildering. How can you make a protected member not be accesible from a derived class? Well, a good start is not making it protected.
protected is by definition exactly what you don't want, so don't use it! Use private instead.
If what you are asking is how to make it a readonly member when accessed from derived types, you have two options:
Declare it as readonly in the base class if possible.
Use a protected property instead with a private setter.
Many novice coders seems to think protected members aren't part of the public surface of the type but they really are, as long as the class can be extended. As such, the rules of public members apply: never expose public fields unless they are readonly or constants, use properties instead.
You should not have classes that implement your generic singleton class.
Otherwise, by default, your protected fields will be accessible by the subclasses (it's what "protected" keyword does)
Instead, you should do something like this:
class Program
{
static void Main(string[] args)
{
var barInstance = Foo<Bar>.GetInstance();
}
}
public class Foo<T> where T : new()
{
protected bool knowsFu;
private static T _instance;
public static T GetInstance()
{
if (_instance == null)
_instance = new T();
return _instance;
}
}
public class Bar
{
public Bar()
{
}
}
Edit 1:
To use a singleton, you should not make another class implement the singleton behavior (This is not how the singleton pattern works).
To use the same classes as your second example, you should do something like this.
public class SingletonBehaviour<T> where T : new()
{
public static T Instance
{
get
{
if(instance == null)
instance = new T()
return instance;
}
}
private static T instance { get; set; }
}
public class APIManager // This class should not inherit from the SingletonBehavior class
{
// Methods like SendHTTPPost(), HTTPGet(), etc.
}
public class ProjectAAPIManager : APIManager
{
public ProjectAAPIManager GetInstance() => SingletonBehavior<ProjectAAPIManager>.Instance();
}

statkc trace with static variable [duplicate]

Given a static class with an initializer method:
public static class Foo
{
// Class members...
internal static init()
{
// Do some initialization...
}
}
How can I ensure the initializer is run before Main()?
The best I can think of is to add this to Foo:
private class Initializer
{
private static bool isDone = false;
public Initializer()
{
if (!isDone)
{
init();
isDone = true;
}
}
}
private static readonly Initializer initializer = new Initializer();
Will this work or are there some unforeseen caveats? And is there any better way of doing this?
Simply do the initialization inside a static constructor for Foo.
From the documentation:
A static constructor is called automatically to initialize the class
before the first instance is created or any static members are
referenced.
There are static constructors in C# that you can use.
public static class Foo
{
// Class members...
static Foo(){
init();
// other stuff
}
internal static init()
{
// Do some initialization...
}
}
Move your code from an internal static method to a static constructor, like this:
public static class Foo
{
// Class members...
static Foo()
{
// Do some initialization...
}
}
This way, you are quite sure that the static constructor will be run on first mention of your Foo class, whether it's a construction of an instance or access to a static member.
Place your initialization code in the static constructor of the class
static Foo()
{
// Initialization code
}
This constructor is run the first time the class is accessed.
You can use RunClassConstructor to trigger the static constructor of the class before using a class. This can be useful if, for instance, this class registers itself in a IOC container or something like this.
RuntimeHelpers.RunClassConstructor(typeof(Foo).TypeHandle);
You find the RuntimeHelpers in the System.Runtime.CompilerServices namespace.
C# 9.0 added Module Initializers allowing you to write initialization code for the assembly with no need to explicitly call anything.
Apply the [ModuleInitializer] attribute to a static void parameterless method to make it a module initializer.
[ModuleInitializer]
internal static void MyAssemblyInitializer()
{
// ...
}

Static Class VS Private Constructor

Today, I have been reading about static class and private constructor.
Static Class - We cannot create an instance on the static class. we cannot inherit the static class. Only single instance is generated.
Private Constructor - We cannot create an instance. We cannot inherit. (I Don't know about how many instance is generated.)
I created two console application i.e. One for static Class, One for Private constructor.
Static Class Code
I understood single object in generated as constructor is called once.
Private Constructor Code
Now, I didn't understand that whether any object is generated or not.
I have two question.
Question 1. I didn't find any particular difference between Private constructor and Static class. Can you please suggest me that in which scenario where I should use Private Constructor and where should I use Static class as I can use both of them.
Question 2. If I use private constructor, how many objects is generated?
Thanks.
EDIT :
I think that people didn't understand my question.
I know that static constructor always call once on the first reference. Static constructor is used to initialize static members of the class.
Question 1. I have a situation : I need to create a class which cannot be instantiated.I can do this by either static class or private constructor. So my question is that "Is there any difference between both of them? which one I should use??"
Question 2. If I use private constructor, how many object is created? If answer is 0 then how private constructor's memory allocation works in the CLR. There is no memory allocation if I use private constructor.
Both of you examples you are calling static methods the difference between the two is that the first method is being called within a static class which cannot be instantiated. The second class could be instantiated however you did not choose to.
The static constructor in the first example is run automatically at runtime when it is needed, it is generally only run once.
The private constructor is never run as you never instantiated a testPrivateConstructor object. not because it is private.
The Edit:
Q1: If you need a class that cannot be instantiated use a static class. Only use a private constructor instead of a static class if you need to initialise static members (or a singleton pattern).
Q2: If you use a private constructor you can instantiate an instance of your class within the class itself so the number of objects that are created depend on how many times you instantiate new objects.
your mixing up a few different things here
a static class public static class MyClass can only contain static elements and never be initialised
a constructor (whether public or private) always creates in instance of the class the public or private only says the visibility of the constructor.
this is commonly used when implementing a singleton design
private MyClass()
{
}
private static MyClass _Singleton;
public static MyClass Singleton
{
get
{
if(_Singleton==null) _Singleton = new MyClass();
return _Singleton
}
}
}
the other is a Class Initialiser, this is a little confusing because its syntax is very similar to a constructor baring the adding of a static keyword and lack of parameters
static MyClass()
{
//configure static variables on first us only
b = //read value from file or other resource not avalable at compile time
a = b.Lenth; //can't be be done in class body as b would not have been initialised yet
}
private static int a;
private static string b;
ergo if your class can't be instantiated then you can only declare is as static nothing else will do that,
if you call a private constructor then every call creates an instance
a class initialiser can never be called its fired automatically on the first use of a class and does not create an instance
EDIT:
here is a revised version of your test program
public static class StaticClassExample
{
public static void ClassFunction()
{
Console.WriteList("This is a class function")
}
}
public static class InitialisedStaticClassExample
{
static InitialisedStaticClassExample()
{
Console.WriteList("This class has been initialised")
}
public static void ClassFunction()
{
Console.WriteList("This is a class function")
}
}
public class PrivateConstuctorClassExample
{
static PrivateConstuctorClassExample()
{
Console.WriteList("This class has been initialised")
}
private PrivateConstuctorClassExample()
{
Console.WriteList("This class has been Instantiated")
}
public static void ClassFunction()
{
Console.WriteList("This is a class function");
var instance = new PrivateConstuctorClassExample();
instance.InstanceFunction();
}
public void InstanceFunction()
{
Console.WriteList("This is a instance function")
}
}
Static constructor will be called first time when the class is
referenced. Static constructor is used to initialize static members
of the class.
In the non static class the private or public constructor will not be
called. Static members will not be initialized either by private or
public constructor.
see the below exmaple
class Program
{
static void Main(string[] args)
{
OnlyOne.SetMyName("I m the only one."); //static constructor will be called first time when the class will be referenced.
Console.WriteLine(OnlyOne.GetMyName());
NoInstance.SetMyName("I have private constructor"); //No constructor will be called when the class will be referenced.
Console.WriteLine(NoInstance.GetMyName());
Console.Read();
}
}
static class OnlyOne
{
static string name;
/// <summary>
/// This will be called first time when even the class will be referenced.
/// </summary>
static OnlyOne()
{
name = string.Empty;
Console.WriteLine("Static constructor is called");
}
public static string GetMyName()
{
return name;
}
public static void SetMyName(string newName)
{
name = newName;
}
}
public class NoInstance
{
static string name;
private NoInstance()
{
name = string.Empty;
Console.WriteLine("No instance private constructor is called");
}
public static string GetMyName()
{
return name;
}
public static void SetMyName(string newName)
{
name = newName;
}
}
}
Question 2: 0
Question 1: you created a static class with a static constructor, but you do not need the instance in your case, since the method is static too and the only thing you need to run a static method is the class definition - no instance.
your second class works also without construction of an instance - the static method does not need one. and since this is a private constructor only class methods could create the instance.
you would use the private constructor only in case of the singleton pattern i guess - without a local caller for private constructor it is rather useless.
Constructor of a class is called upon creation of instance of the class.
Static constructors are called on initialization of the class. Read this
In example 1, your static constructor is initialized the as you are accessing a static method of the class. This is relevant for a static or non-static class.
In example 2, your private constructor isn't static.
Your constructor wasn't called as there was no instance created.
It has to be called by creating an instance of the class.
Private constructors are used to control the construction and destruction of the instance of the class.
Since, only the class can call its constructor in this case. You need a static method to get the instance of the class
For example,
public class TestPrivateConstructor
{
private TestPrivateConstructor()
{
Console.WriteLine("Instance is created, Private Constructor called");
}
static TestPrivateConstructor _instance;
public static TestPrivateConstructor GetInstance()
{
if(_instance == null)
{
_instance = new TestPrivateConstructor();
}
return _instance;
}
public static void DisposeInstance()
{
if(_instance !=null)
{
_instance.Dispose();
_instance = null;
}
}
public void TestMethod()
{
Console.WriteLine("Test MEthod Called");
}
void Dispose()
{
//Do something
}
}
For the code above, try using this. Now your private constructor is called as you created an instance.
class Program
{
public static void Main(string[] args)
{
//Private constructor
TestPrivateConstructor.GetInstance()
}
}
Using the approach above, you can control the construction and destruction of the object.
If you still have any questions, please feel free to ask.

C# static class constructor

Is there a work around on how to create a constructor for static class?
I need some data to be loaded when the class is initialized but I need one and only one object.
C# has a static constructor for this purpose.
static class YourClass
{
static YourClass()
{
// perform initialization here
}
}
From MSDN:
A static constructor is used to initialize any static data, or to
perform a particular action that needs to be performed once only. It
is called automatically before the first instance is created or any
static members are referenced
MSDN link
.
A static constructor looks like this
static class Foo
{
static Foo()
{
// Static initialization code here
}
}
It is executed only once when the type is first used. All classes can have static constructors, not just static classes.
Yes, a static class can have static constructor, and the use of this constructor is initialization of static member.
static class Employee1
{
static int EmpNo;
static Employee1()
{
EmpNo = 10;
// perform initialization here
}
public static void Add()
{
}
public static void Add1()
{
}
}
and static constructor get called only once when you have access any type member of static class with class name Class1
Suppose you are accessing the first EmployeeName field then constructor get called this time, after that it will not get called, even if you will access same type member.
Employee1.EmployeeName = "kumod";
Employee1.Add();
Employee1.Add();
Static constructor called only the first instance of the class created.
like this:
static class YourClass
{
static YourClass()
{
//initialization
}
}
We can create static constructor
static class StaticParent
{
StaticParent()
{
//write your initialization code here
}
}
and it is always parameter less.
static class StaticParent
{
static int i =5;
static StaticParent(int i) //Gives error
{
//write your initialization code here
}
}
and it doesn't have the access modifier
You can use static constructor to initialization static variable. Static constructor will be entry point for your class
public class MyClass
{
static MyClass()
{
//write your initialization code here
}
}
Static classes cannot have instance constructors (unlike the accepted answer). However, a class can have a static constructor. That is totally different.

Categories