《刻意练习之C#》-0002- Main方法

时间:2020-05-31 21:49:31   收藏:0   阅读:86

main方法简介

main方法的定义

public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }

多个Main方法

using System;

namespace _0002main_method
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"this Main method in Program class");
        }
    }

    class Hello
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"this Main method in Hello class");
        }
    }
}

当运行程序(dotnet run)时报错:

Hello.cs(7,21): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. The build failed. Fix the build errors and run again.

 

多Main方法-解决办法  

使用csc编译

csc Program.cs /main:_0002main_method.Hello

使用dotnet build

dotnet build /p:StartupObject=_0002main_method.Hello

补充说明

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!