using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace test2
{
class Program
{
static void Main(string[] args)
{
//什么是泛型List
List<string> str_list = new List<string>() { "aaa", "dd", "123123" };
//Object,有拆箱装箱
List<object> obj1 = new List<object> { true, new Person(), "123" };
foreach (var item in str_list)
{
Console.WriteLine(item);
}
//泛型Dictionary
Dictionary<int, object> dc1 = new Dictionary<int, object>();
dc1.Add(1, new Person());
dc1.Add(2, new Person());
//自定义泛型,通用性
MyApp<string> myapp1 = new MyApp<string>("这里是自定义泛型");
myapp1.show();
MyApp<int> myapp2 = new MyApp<int>(111);
myapp2.show();
//泛型方法
sayT("88888");
}
public class Person
{
}
//泛型静态方法
public static void sayT<T>(T t)
{
Console.WriteLine(t.ToString());
}
public class MyApp<T> //参数可以写多个
{
private T t;
public MyApp(T t)
{
this.t = t;
}
public void show()
{
Console.WriteLine(t);
}
}
}
}
网友评论