using System;
using System.Collections.Generic;
namespace ConsoleApplication74
{
class Program<T>
{
public void Add(T X)
{
Console.WriteLine("{0}", X);
}
static void Main(string[] args)
{
Program<string> MyGeneric = new Program<string>();
MyGeneric.Add("ABC");
Console.Read();
}
}
我有错误Program does not contain a static 'Main' method suitable for an entry point
。
Program.cs属性具有Build Action as Compile。
我不知道出了什么问题。
答案 0 :(得分:1)
Main
方法或程序中的入口点不能位于具有泛型参数的类中。您的Program
类具有T
类型参数。 C#规范在应用程序启动下的3.1节中调用它:
应用程序入口点方法可能不在泛型类声明中。
您应该创建一个新类,而不是尝试使用Program
:
class Program
{
static void Main(string[] args)
{
MyClass<string> MyGeneric = new MyClass<string>();
MyGeneric.Add("ABC");
Console.Read();
}
}
class MyClass<T>
{
public void Add(T X)
{
Console.WriteLine("{0}", X);
}
}