Main的入口点错误(字符串args)?

Nan*_* HE 7 c# syntax

使用系统; 使用System.Collections.Generic; 使用System.Text;

namespace MyConApp
{
    class Program
    {
        static void Main(string args) 
        {
            string tmpString; 
            tmpString = args;
            Console.WriteLine("Hello" + tmpString);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么下面的表达式显示编译错误消息"不包含适用于入口点的静态'Main'方法"

namespace MyConApp
{
    class Program
    {
        static void Main(string args) 
        {
            string tmpString; 
            tmpString = args;
            Console.WriteLine("Hello" + tmpString);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

ZEE*_*ZEE 6

在您提供的代码中,问题在于“主”入口点期望在调用程序时从系统传递一个字符串数组(此数组可以为null,没有元素)

纠正改变

static void Main(string args) 
Run Code Online (Sandbox Code Playgroud)

static void Main(string[] args) 
Run Code Online (Sandbox Code Playgroud)

如果您声明“ void”或“ int”以外的任何类型的“ Main”,则可能会遇到相同的错误

因此,“ Main”方法的签名必须始终是

static // ie not dynamic, reference to method must exist
public // ie be accessible from the framework invoker
Main   // is the name that the framework invoker will call

string[] <aName> // can be ommited discarding CLI parameters
* is the command line parameters space break(ed)
Run Code Online (Sandbox Code Playgroud)

从MS(...),Main方法可以使用参数,在这种情况下,它采用以下形式之一:

static int Main(string[] args)
static void Main(string[] args)
Run Code Online (Sandbox Code Playgroud)


San*_*nds 5

因为参数是String而不是预期的String Array


sha*_*esh 4

请参阅此内容以了解 Main方法签名选项。