// 錯誤:找不到 T, S
public class TestChild : Test { }
// 正確:父類別具體化
public class TestChild : Test { }
// 正確:父子同參數
public class TestChild : Test { }
// 正確:子類別泛型,父類別具體化
public class TestChild : Test { }
public interface IList { T[] GetElements(); }
public interface IDictionary { void Add(K key, V value); }
// 實作:型別參數具體化或來源於實作類別
class List : IList, IDictionary
{
public T[] GetElements() => null;
public void Add(int index, T value) { }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericTest
{
// 委派型別參數用於參數與回傳值
delegate string GenericDelegate(T value);
class Test
{
static string F(int i) => "SHY520";
static string G(string s) => "SHY520";
static void Main(string[] args)
{
GenericDelegate G1 = G;
GenericDelegate G2 = new GenericDelegate(F);
}
}
}
泛型委派支援回傳值與參數套用型別參數。