简体   繁体   English

没有main方法的C#类

[英]C# class without main method

I'm learning C# and I'm very new to it, so forgive me for the seemingly stupid question.我正在学习 C# 并且我对它很陌生,所以请原谅我这个看似愚蠢的问题。 I have some experience in Java, and I noticed that C# programs also need a main() method in their main class.我在 Java 方面有一些经验,我注意到 C# 程序在它们的主类中也需要一个main()方法。

What if I want to create a class that isn't a main class, ie 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.我尝试这样做,当我编译(通过 cmd 使用csc File.cs )时,编译器说它将生成的 .exe 没有main()方法。 Does that mean that I was wrong, and every class needs a main() method, or that I'm compiling it wrongly?这是否意味着我错了,每个类都需要一个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 经验),但是每当我尝试在没有一个的情况下进行编译时,编译器都会告诉我我需要一个。


Not all classes need Main method.并非所有类都需要Main方法。

As MSDN States作为 MSDN 状态

The Main method is the entry point of a C# console application or windows application. Main 方法是 C# 控制台应用程序或 Windows 应用程序的入口点。 (Libraries and services do not require a Main method as an entry point.). (库和服务不需要 Main 方法作为入口点。)。 When the application is started, the Main method is the first method that is invoked.当应用程序启动时,Main 方法是第一个被调用的方法。

There can only be one entry point in a C# program. C# 程序中只能有一个入口点。 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 方法的类,则必须使用 /main 编译器选项编译您的程序,以指定使用哪个 Main 方法作为入口点。


Only one class need to keep the Main method, 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() 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();
    }
}

In this scenario you need at least one class in your code with the Main method in it.在这种情况下,您的代码中至少需要一个包含Main方法的类。 The other classes do not need the Main method.其他类不需要Main方法。

You only need a main method when you build an executable assembly (.exe), and you only need it in one class.构建可执行程序集(.exe)时只需要一个main方法,并且只在一个类中需要它。 This method will be the default entry point where execution starts.此方法将是执行开始的默认入口点。 You don't need a main method when you build your code as a class library (.dll).将代码构建为类库 (.dll) 时,不需要main方法。

Try using /t:library switch with the compiler.尝试在编译器中使用/t:library开关。 By default it tries to make an .exe which, of course, needs an entry point (ie a main method).默认情况下,它会尝试创建一个.exe ,当然,它需要一个入口点(即main方法)。 If you compile to a .dll you won't need that.如果您编译为.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(如果您还没有,请下载免费版本之一)并让它担心编译器标志。

C# application must have at least one class with Main method, so that execution can begin from it. C#应用程序必须至少有一个带有 Main 方法的类,以便可以从它开始执行。 Application can have plenty of classes, but only one class with only one Main method is required.应用程序可以有很多类,但只需要一个只有一个 Main 方法的类。

C# library does not have to have a Main method. C#不必具有 Main 方法。

If this is a console application, you need a Main method to start with.如果这是一个控制台应用程序,您需要一个 Main 方法来开始。 Otherwise (web application, for example), you don't need one.否则(例如 Web 应用程序),您不需要一个。

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 中设置启动对象。

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 文件

static void main(string[] args) method in C# programs is the start point to execute. C# 程序中的static void main(string[] args)方法是执行的起点。 If you try to compile a single C# File, the compiler will find this method to start the execution.如果您尝试编译单个 C# 文件,编译器将找到此方法开始执行。 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...您不需要在用作模型的类中创建该方法,但是您必须在控制台程序、WinForms 等上使用此方法...

Main is required, but C#9 has this feature such that you will not write Main but it will act as Main. Main 是必需的,但 C#9 具有此功能,因此您不会编写 Main,但它将充当 Main。

In C# 9.0 you can just choose to write your main program at the top level instead:在 C# 9.0 中,您可以选择在顶层编写主程序:

using System;

Console.WriteLine("Hello World!");

Any statement is allowed.任何声明都是允许的。 The program has to occur after the usings and before any type or namespace declarations in the file, and you can only do this in one file, just as you can have only one Main method today.该程序必须发生在 using 之后和文件中的任何类型或命名空间声明之前,并且您只能在一个文件中执行此操作,就像今天只能有一个 Main 方法一样。

If you want to return a status code you can do that.如果你想返回一个状态码,你可以这样做。 If you want to await things you can do that.如果你想等待事情,你可以这样做。 And if you want to access command-line arguments, args is available as a “magic” parameter.如果您想访问命令行参数,args 可用作“魔术”参数。

Local functions are a form of statement and are also allowed in the top-level program.局部函数是一种语句形式,也允许在顶级程序中使用。 It is an error to call them from anywhere outside of the top-level statement section.从顶级语句部分之外的任何地方调用它们都是错误的。

Reference : https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#top-level-programs参考https : //devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#top-level-programs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM