using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { delegate T NumberChanger<T>(T n); class Program { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int p) { num *= p; return num; } static void Main(string[] args) { MyGenericArray<int,char> intArray = new MyGenericArray<int,char>(5); for (int c = 0; c < 5; c++) { intArray.SetItem(c, c * 5); } for (int c = 0; c < 5; c++) { Console.Write(intArray.GetItem(c)+" "); } intArray.GenericMethod<string>("heoooooo"); intArray.GenericMethod<int>(100); MyGenericArray<char,string > charArray = new MyGenericArray<char,string >(5); for (int c = 0; c < 5; c++) { charArray.SetItem(c, (char)(c+97)); } for (int c = 0; c < 5; c++) { Console.Write(charArray.GetItem(c) + " "); } int a, b; char cc, d; a = 10; b = 20; cc = ‘I‘; d = ‘v‘; Console.WriteLine("a:{0};b:{1}",a,b); Console.WriteLine("cc:{0};d:{1}", cc, d); Swap<int>(ref a, ref b); Swap<char>(ref cc, ref d); Console.WriteLine("a:{0};b:{1}", a, b); Console.WriteLine("cc:{0};d:{1}", cc, d); NumberChanger<int> nc1 = new NumberChanger<int>(AddNum); NumberChanger<int> nc2 = new NumberChanger<int>(MultNum); nc1(25); Console.WriteLine(num); nc2(5); Console.WriteLine(num); Console.ReadKey(); } public static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; } } public class MyGenericArray<T,K> where T:struct { private T[] array; public MyGenericArray(int size) { array = new T[size + 1]; } public T GetItem(int index) { return array[index]; } public void SetItem(int index, T value) { array[index] = value; } public void GenericMethod<X>(X x) { Console.WriteLine(x.ToString()); } } //public : MyGenericAr《y <int> //{ //} //public class SubGenerClass<T>:MyGenericArray <T> where T;struct //{ //} }
时间: 2024-09-30 00:32:48