2016-09-12 33 views
1
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(); 
    } 
} 

我有erroe Program does not contain a static 'Main' method suitable for an entry point。 Program.cs屬性的Build Action爲Compile。 我不知道什麼是錯的。通用列表和靜態主要

+0

的可能的複製[爲什麼我不能在C#應用程序的通用型入口點使用?(HTTP://計算器.com/questions/28045923/why-i-can-use-as-entry-point-in-c-sharp-app-a-generic-type) – hatchet

回答

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); 
    } 
} 
+0

那麼如何使用我自己的泛型? –

+0

非常感謝。當我看到你的第一個答案時,我只是在我的腦海中有了這個解決方案謝謝! –