Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Program class be static? [duplicate]

I am getting the following Warning in Visual Studio 2019, after creating a new ASP.NET Core 3 project:

Warning CA1052 Type 'Program' is a static holder type but is neither static nor NotInheritable

public class Program
    {
        public static void Main(string[] args)
        {
            // ...
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            // ...
    }

vs

public static class Program
    {
        public static void Main(string[] args)
        {
            // ...
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            // ...
    }

Should I add the static modifier? Why / Why not? Pro's and Cons'?

Edit: This is a ASP.NET Core 3 API

like image 361
Nelson Sousa Avatar asked Oct 22 '19 22:10

Nelson Sousa


People also ask

Does program Cs need to be static?

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.

When should a class be static?

A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, in the . NET Class Library, the static System.

Can program class be static in C#?

In C#, one is allowed to create a static class, by using static keyword. A static class can only contain static data members, static methods, and a static constructor.It is not allowed to create objects of the static class. Static classes are sealed, means you cannot inherit a static class from another class.

What is the use of declaring a class as static?

We can declare a class static by using the static keyword. 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.


Video Answer


1 Answers

In more basic terms the message could be imagined to say:

Your class 'Program' only seems to contain methods that are declared as static and as a result it can not participate in an inheritance hierarchy. Declare it as static (or sealed if you're targeting an ancient version of .net that doesn't support static classes) to more accurately reflect what its design sentiment is

It's a recommendation, to mark your class as static because it only contains static stuff. This will prevent anyone making the mistake of trying to inherit from it and thinking they can then do something useful inheritance-wise with the inherited version

Microsoft don't mark it as static for you because there's nothing special about Program per se; you could put non static methods in it, or your could put your static void Main in another class, like Person, that IS instantiable.

class Person{
  public string Name {get;set;}
  static void Main(){
    Person p = new Person();
    p.Name = Console.ReadLine();
  }
}

This would work fine; a class does not have to be static to host the application entry point and in this case the class couldn't be static because it has non static members. It can be (and is, in the main) instantiated in the main. It's not called Program; there isn't a class called Program anywhere and this tiny app will still run (doesn't do much..)

In your case, either do as recommended and add a static modifier to your class, because it will make your program that bit more robustly engineered, or add an instance member if you can think of a valid reason for instantiating Program, or ignore the message and carry on with your non static class that contains only static methods - it'll still work

like image 199
Caius Jard Avatar answered Oct 06 '22 06:10

Caius Jard