c#:控制台应用程序 - 静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1685692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:59:02  来源:igfitidea点击:

c# : console application - static methods

c#static

提问by dotnet-practitioner

why in C#, console application, in "program" class , which is default, all methods have to be static along with

为什么在 C#,控制台应用程序中,在默认的“程序”类中,所有方法都必须是静态的

static void Main(string[] args)

采纳答案by Mark Rushakoff

Member functions don't haveto be static; but if they are notstatic, that requires you to instantiate a Programobject in order to call a member method.

成员函数不具有是静态的; 但如果它们不是静态的,则需要您实例化一个Program对象才能调用成员方法。

With static methods:

使用静态方法:

public class Program
{
    public static void Main()
    {
        System.Console.WriteLine(Program.Foo());
    }

    public static string Foo()
    {
        return "Foo";
    }
}

Without static methods (in other words, requiring you to instantiate Program):

没有静态方法(换句话说,需要您实例化Program):

public class Program
{
    public static void Main()
    {
        System.Console.WriteLine(new Program().Foo());
    }

    public string Foo() // notice this is NOT static anymore
    {
        return "Foo";
    }
}

Mainmust be static because otherwise you'd have to tell the compiler how to instantiate the Programclass, which may or may not be a trivial task.

Main必须是静态的,否则您必须告诉编译器如何实例化Program该类,这可能是也可能不是一项微不足道的任务。

回答by Arsen Mkrtchyan

You can write non static methods too, just you should use like this

您也可以编写非静态方法,只是您应该像这样使用

static void Main(string[] args)
{
    Program p = new Program();
    p.NonStaticMethod();
}

The only requirement for C# application is that the executable assembly should have one static main method in any class in the assembly!

C# 应用程序的唯一要求是可执行程序集在程序集中的任何类中都应该有一个静态 main 方法!

回答by John K

The Main method is static because it's the code entry point to the assembly. There is no instance of any object at first, only the class template loaded in memory and its static members including the Main entry point static method. Main is predefined by the C# compiler to be the entry point.

Main 方法是静态的,因为它是程序集的代码入口点。起初没有任何对象的实例,只有加载到内存中的类模板及其静态成员,包括 Main 入口点静态方法。Main 由 C# 编译器预定义为入口点。

A static method can only call other static methods (unless there is an instance handle of something composited for use). This is why the Main method calls other static methods and why you get a compile error if you try to call a non-static (instance) method.

一个静态方法只能调用其他静态方法(除非有组合使用的实例句柄)。这就是 Main 方法调用其他静态方法的原因,以及如果尝试调用非静态(实例)方法时会出现编译错误的原因。

However, if you create an instance of any class, even of the Program class itself, then you start creating objects in your application on the heap area of memory. You can then start calling their instance members.

但是,如果您创建任何类的实例,甚至是 Program 类本身的实例,那么您就开始在应用程序中的内存堆区域上创建对象。然后您可以开始调用他们的实例成员。

回答by Carlos Mu?oz

Not all methods have to be static, you can add instance methods and also create an instance of your Program class.
But for Main it has to be static beacause it's the entry point of your application and nobody is going to create an instance and call it.

并非所有方法都必须是静态的,您可以添加实例方法并创建 Program 类的实例。
但是对于 Main 来说,它必须是静态的,因为它是应用程序的入口点,没有人会创建一个实例并调用它。

回答by Vitaly Stakhov

So, technically correct answers are above :)

所以,技术上正确的答案在上面:)

I should point out that generally you don't want to go in the direction of all static methods. Create an object, like windows form, a controller for it and go towards object-oriented code instead on procedural.

我应该指出,通常您不想朝着所有静态方法的方向发展。创建一个对象,如窗体,它的控制器,然后转向面向对象的代码而不是过程。