没有main方法的C#类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14688756/
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-10 12:40:44  来源:igfitidea点击:

C# class without main method

c#classmain

提问by Bluefire

I'm learning C# and I'm very new to it, so forgive me for the seemingly stupid question. I have some experience in Java, and I noticed that C# programs also need a main()method in their main class.

我正在学习 C# 并且我对它很陌生,所以请原谅我这个看似愚蠢的问题。我在 Java 方面有一些经验,我注意到 C# 程序main()在其主类中也需要一个方法。

What if I want to create a class that isn't a main class, i.e. one that I import into a main class?

如果我想创建一个不是主类的类,即我导入到主类中的类怎么办?

I tried to do that, and when I compile (via cmd using csc File.cs) the compiler says that the .exe that it will make has no main()method. Does that mean that I was wrong, and everyclass needs a main()method, or that I'm compiling it wrongly?

我尝试这样做,当我编译(通过 cmd using csc File.cs)时,编译器说它将生成的 .exe 没有main()方法。这是否意味着我错了,每个类都需要一个main()方法,或者我编译错了?

Maybe the problem's in the code (since I'm relying on my knowledge of Java syntax), which looks like this:

也许问题出在代码中(因为我依赖于我对 Java 语法的了解),如下所示:

public class Class
{
    int stuff;
    public Class(int stuff)
    {
        this.stuff = stuff;
        stuff();
    }
    public void method()
    {
        stuff();
    }
}

EDIT:I'm afraid this is terribly misunderstood. I'm not asking if the file needs a main method, I'm asking how I can import this class into another class, because I realise that if I am to do this I can't have a main (as I said, I have some Java experience), but whenever I try to compile without one the compiler tells me that I need one.

编辑:恐怕这是非常误解。我不是问文件是否需要一个 main 方法,我问的是如何将这个类导入另一个类,因为我意识到如果我要这样做,我不能有一个 main(正如我所说,我有一些 Java 经验),但是每当我尝试在没有一个的情况下进行编译时,编译器都会告诉我我需要一个。

采纳答案by Parimal Raj



Not all classes need Mainmethod.

不是所有的类都需要Main方法。

As MSDN States

作为 MSDN 状态

The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.

There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point.

Main 方法是 C# 控制台应用程序或 Windows 应用程序的入口点。(库和服务不需要 Main 方法作为入口点。)。当应用程序启动时,Main 方法是第一个被调用的方法。

C# 程序中只能有一个入口点。如果您有多个具有 Main 方法的类,则必须使用 /main 编译器选项编译您的程序,以指定使用哪个 Main 方法作为入口点。



Only one class need to keep the Mainmethod, the class which acts as entry point of the application.

只有一个类需要保留Main方法,该类充当应用程序的入口点。

The signature of the main method is : static void Main(string[] args)or static void Main()or static int Main(string[] args)or static int Main()

主要方法的签名是:static void Main(string[] args)static void Main()static int Main(string[] args)static int Main()

Check out this link for more details : Main() and Command-Line Arguments (C# Programming Guide)

看看这个链接查看更多细节:Main() and Command-Line Arguments (C# Programming Guide



For your above example:

对于你上面的例子:

public class MyClassName // changed the class name, avoid using the reserved keyword :P
{
    int stuff;
    public MyClassName(int stuff)  // is the constructor
    {
        this.stuff = stuff;
    }
    public void method()
    {
        stuff = 1;
    }
}

If you need to use that class, you can create a static class with main method:

如果您需要使用该类,您可以使用 main 方法创建一个静态类:

class ProgramEntry
{
    static void Main(string[] args)
    {
        MyClassName classInstance = new MyClassName(2);
        classInstance.method();
    }
}

回答by Felipe Oriani

static void main(string[] args)method in C# programs is the start point to execute. If you try to compile a single C# File, the compiler will find this method to start the execution. You don't need to create the method in a class you are using as a model, but you have to have this method on a Console Program, WinForms, etc...

static void main(string[] args)C#程序中的方法是执行的起点。如果您尝试编译单个 C# 文件,编译器将找到此方法开始执行。您不需要在用作模型的类中创建该方法,但是您必须在控制台程序、WinForms 等上使用此方法...

回答by chue x

In this scenario you need at least one class in your code with the Mainmethod in it. The other classes do not need the Mainmethod.

在这种情况下,您的代码中至少需要一个包含Main方法的类。其他类不需要该Main方法。

回答by Botz3000

You only need a mainmethod when you build an executable assembly (.exe), and you only need it in one class. This method will be the default entry point where execution starts. You don't need a mainmethod when you build your code as a class library (.dll).

main构建可执行程序集(.exe)时只需要一个方法,并且只在一个类中需要它。此方法将是执行开始的默认入口点。main将代码构建为类库 (.dll) 时不需要方法。

回答by Matt Burland

Try using /t:libraryswitch with the compiler. By default it tries to make an .exewhich, of course, needs an entry point (i.e. a mainmethod). If you compile to a .dllyou won't need that.

尝试/t:library在编译器中使用switch。默认情况下,它会尝试创建一个.exe,当然,它需要一个入口点(即一个main方法)。如果你编译成 a.dll你就不需要那个了。

But as HighCore suggested, if you are learning, just use Visual Studio (download one of the free versions if you haven't already) and let it worry about the compiler flags.

但是正如 HighCore 建议的那样,如果您正在学习,只需使用 Visual Studio(如果您还没有,请下载免费版本之一)并让它担心编译器标志。

回答by Dialecticus

C# applicationmust have at least one class with Main method, so that execution can begin from it. Application can have plenty of classes, but only one class with only one Main method is required.

C#应用程序必须至少有一个带有 Main 方法的类,以便可以从它开始执行。应用程序可以有很多类,但只需要一个只有一个 Main 方法的类。

C# librarydoes not have to have a Main method.

C#不必具有 Main 方法。

回答by Robert

If this is a console application, you need a Main method to start with. Otherwise (web application, for example), you don't need one.

如果这是一个控制台应用程序,您需要一个 Main 方法来开始。否则(例如 Web 应用程序),您不需要一个。

回答by kittu

Only one class with one method should be fine. If you want, you can set up the start up object in visual studio in the settings.

只有一个具有一种方法的类应该没问题。如果需要,您可以在设置中的 Visual Studio 中设置启动对象。

回答by Abdul Karim Siddiqui

C# class without Main() means, you can compile it as a library (.dll) csc /target:library YourClassFileName.cs or csc /t:library YourClassFileName.cs to make it as YourClassFileName.dll file and then you can use it in another class file which have the Main() method (Entry point)

没有 Main() 的 C# 类意味着,您可以将其编译为库 (.dll) csc /target:library YourClassFileName.cs 或 csc /t:library YourClassFileName.cs 使其成为 YourClassFileName.dll 文件,然后您可以使用它在另一个具有 Main() 方法的类文件中(入口点)

csc /reference:YourClassFileName.cs YourMainClassFileName.cs or csc /r:YourClassFileName.cs YourMainClassFileName.cs

csc /reference:YourClassFileName.cs YourMainClassFileName.cs 或 csc /r:YourClassFileName.cs YourMainClassFileName.cs

to make an YourMainClassFileName.exe file

制作一个 YourMainClassFileName.exe 文件