Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the class "Program" declared as static?

Tags:

When you create a WinForm App, you get an automatically generated template of Program class in the Program.cs file.

Which looks something like this:

static class Program {     /// <summary>     /// The main entry point for the application.     /// </summary>     [STAThread]     static void Main()     {         Application.EnableVisualStyles();         Application.SetCompatibleTextRenderingDefault(false);         Application.Run(new Form1());     } } 

My question is, why is the Program class declared as static?
If I remove the static declaration it still works fine.

The reason for the question is that I thought it could be nice to have Program inherit from a base class that would implement handling of Application.ThreadException and AppDomain.CurrentDomain.UnhandledException instead of implementing it more or less the same for all of my projects.

like image 273
Avi Turner Avatar asked Sep 30 '13 10:09

Avi Turner


People also ask

Why would you define a class to be static?

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor.

Why is a class declared static in Java?

A class can be declared static only if it is a nested class. It does not require any reference of the outer class. The property of the static class is that it does not allows us to access the non-static members of the outer class.

What happens when we declare a class as static?

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.

Can a class be declared as static?

Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class in which the nested class is defined is known as the Outer Class.


1 Answers

It just follows a design guideline, that classes that contain only static methods should be marked as static. More information can be found here.

There is no problem in making your Program class not static, the only thing that is required is the static Main method as an entry point, as you've already noticed. You can add instance methods to the Program class, instantiate it and use as any other class. However this is not a clear way, as this violates the Single Responsibility Principle. Program's responsibility is to provide an entry point for the application, so it shouldn't do anything more. For this task it needs only one static method called Main. And as it contains only static methods, it should be marked as static to conform to C# coding guidelines.

In general, it is convenient to know that a class is static, so you know at first glance that it contains only static methods. It is a very readable way of expressing how a class should be used. In this example it isn't very essential, as no one uses Program explicitly, however for the sake of strictness it should be static.

like image 75
BartoszKP Avatar answered Oct 05 '22 16:10

BartoszKP