程序类应该是静态的吗? [英] Should Program class be static?

查看:73
本文介绍了程序类应该是静态的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建新的ASP.NET Core 3项目之后,我在Visual Studio 2019中收到以下警告:

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

警告CA1052 类型程序"是静态持有人类型,但既不是静态也不是NotInheritable

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'?

这是ASP.NET Core 3 API

This is a ASP.NET Core 3 API

推荐答案

在更基本的术语中,该消息可以想象为:

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

您的程序"类似乎仅包含声明为静态的方法,因此它不能参与继承层次结构.将其声明为静态(或密封,如果您的目标是不支持静态类的旧版.net)则可以更准确地反映其设计意图

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不会为您将其标记为静态,因为Program本身没有什么特别之处.您可以在其中放置非静态方法,也可以将static void Main放在可实例化的另一个类(如Person)中.

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();
  }
}

这会很好;一个类不必是静态的才能承载应用程序入口点,在这种情况下,该类不能是静态的,因为它具有非静态成员.可以在主体中实例化(并且在主体中).它不称为程序;那里没有一个叫做Program的类,这个小应用程序仍然可以运行(不会做很多.)

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..)

在您的情况下,请按照建议的方式进行操作,并在类中添加一个静态修饰符,因为这将使您的程序设计得更加健壮,或者,如果您想到了实例化Program的正当理由,请添加一个实例成员,或者忽略该消息并继续使用仅包含静态方法的非静态类-它仍然可以工作

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

这篇关于程序类应该是静态的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆