流浪のwolf

卷帝

导航

泛型dotnet

  // 什么是泛型List<T>  T:表示类型参数,指代任意类型 T可以是任意标识
  // 编写代码时使用特殊符号替代位置类型,在实例化或使用/调用时才会进行具体类型的定义
  // 特点:重用代码,保护类型安全性,提高性能

  // 泛型集合<k,v>
  Dictionary<int, string> directory = new Dictionary<int, string> { { 1, "test"  }, { 2, "test123" } };
  // 集合添加
  directory.Add( 3,"asd" );

 

字符串类型  --  只能 show 字符串类型的数据

public class testClass
{
    public string Name;

    public testClass(string name)
    {
        Name = name;
    }
    public void show()
    {
        Console.WriteLine(Name);
    }
}

泛型类型  --  可以在不改变代码的情况下 show 不同类型的数据

public class testClass<T>
{
    public T Name;

    public testClass(T name)
    {
        Name = name;
    }
    public void show()
    {
        Console.WriteLine(Name);
    }
}

  

 

posted on 2024-05-03 17:16  流浪のwolf  阅读(3)  评论(0编辑  收藏  举报