編譯器錯誤 CS0308
非泛型類型或方法 'identifier' 不能配合類型引數使用。
方法或類型不是泛型,但搭配類型引數使用。 若要避免這個錯誤,請移除角括弧和類型引數,或是將方法或類型重新宣告為泛型方法或類型。
下列範例會產生 CS0308:
// CS0308a.cs
class MyClass
{
public void F() {}
public static void Main()
{
F<int>(); // CS0308 – F is not generic.
// Try this instead:
// F();
}
}
下列範例也會產生 CS0308。 若要解決此錯誤,請使用指示詞 "using System.Collections.Generic"。
// CS0308b.cs
// compile with: /t:library
using System.Collections;
// To resolve, uncomment the following line:
// using System.Collections.Generic;
public class MyStack<T>
{
// Store the elements of the stack:
private T[] items = new T[100];
private int stack_counter = 0;
// Define the iterator block:
public IEnumerator<T> GetEnumerator() // CS0308
{
for (int i = stack_counter - 1 ; i >= 0; i--)
yield return items[i];
}
}