Program issue which belongs to Static Words in C#
Solution 1 -
Listen first, If we see the difference between a non-static and a static class in C# is simply that a static class cannot be instantiated. This means that you don’t use the new keyword to instantiate an instance of the class. Instead, you will access members of the static class by using the class name. Basically, A static class is intended to be a container for a set of methods that only work with supplied parameters and don’t have a need to store or retrieve data that is unique to them. An example of a static class that’s built into the .NET Framework is the System.Math class. It contains methods which perform various mathematical operations.
A static class is like a regular class that only has static members and has a private constructor. If you have a class like this in your design it’s best to make it a static class in order to use the features in the compiler which will insure that instances of the class cannot be created or inherited. By taking this step it will make your code more solid.
Sometimes there is confusion between a static class and a singleton class where only one instance is allowed. The way to separate them is to understand that the static class should not have any internal variables, each method should function independently, like a function library. However, a singleton class may expose internal values as properties and allow the storage and retrieval of these values.
As I mentioned above static classes cannot be instantiated. Instead, the Framework will call the private constructor for the static class prior to the first place the class is referenced in code. This allows you to go in your function library smoothly into your program so if you had a static class like this…
public static class MyFunctions
{
public static string Test1(string myValue)
{
// Code
}
public static int Test2(int myValue)
{
// Code
}
}
…you can call it like so…
MyFunctions.Test1(CurrentValue);
…without having to create an instance of the object using the new keyword (which you can’t do with a static class anyway). Also, a static class will remain in memory for the lifetime of the application domain where it is created.
As you can see in the example above, a static class can only contain static members. The program won’t compile if you don’t remember to mark each of them as static. Also, static classes are sealed, which means that they cannot be inherited and cannot inherit from any class other than System.Object. While a static class cannot have an instance constructor, they can have a private, static, constructor. This routine will be called when the Framework creates the instance of the object.
One thing to bear in mind here is that if your static class requires a lot of extensive initialization it may not be a good candidate for a static class. In my view, these classes are intended for groups of related, but essentially standalone, functions. Having a substantial initialization routine may indicate that the class might work better as a singleton class or as a regular class. Taking this into consideration early on can prevent you from having to recode a static class into something else.