【问题标题】:Generic list and static main通用列表和静态 main
【发布时间】:2016-09-12 19:42:33
【问题描述】:
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 属性具有编译操作。 我不知道出了什么问题。

【问题讨论】:

标签: c#-4.0 generic-list


【解决方案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);
    }   
}

【讨论】:

  • 那么我该如何使用我自己的泛型类型呢??
  • 非常感谢。当我看到你的第一个答案时,我脑子里只有这个解决方案。谢谢!
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 2013-05-28
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 2011-12-15
相关资源
最近更新 更多