• 论泛型


    泛型:顾名思义泛泛的数据类型,也就是不确定的意思。

    泛型集合:对所保存的元素进行类型约束,在调用添加值类型时无需拆箱装箱,避免了许多不必要的操作!

    常用的泛型集合:List<T>和Dictionary<K,V>。

    ADO.NET中我们可以通过编写SQL语句来对程序中的数据进行增.删.改.查等等操作,那么泛型集合中的元素可以进行增.删.改.查等操作吗?

    一:List<T>

    当然可以

    增:

    使用集合的Add()方法

    方法一:

     1  Person p1 = new Person();
     2             p1.name = "张三";
     3             p1.id = "20090101";
     4             p1.sex = "";
     5             p1.age=20;
     6             Person p2 = new Person();
     7             p2.name = "李四";
     8             p2.id = "20090102";
     9             p2.sex = "";
    10             p2.age = 20;
    11             list.Add(p1);
    12             list.Add(p2);

    方法二:使用构造函数赋值

     1 class Person
     2 {
     3         //构造函数
     4         public Person(string grade01, int earth03, string fight01)
     5         {
     6             grade = grade01;
     7             earth = earth03;
     8             fight = fight01;
     9         }

    10 }

      

     1     static void Main(string[] args)
     2         {
     3              Person p2 = new Person("90级", 500000, "8999");
     4             list.Add(p2);
     5             foreach (Person item in list)
     6             {
     7                 Console.WriteLine("巫师的生命值是:{0},战斗力是:{1},       等级是{2}", item.Earth, item.Fight, item.Grade);
     8             }
     9             Console.ReadLine();
    10 }

    删:

    使用Remove()和RemoveAt()方法。

    使用foreach遍历集合,找到满足条件的一项进行删除

     1   string id=this.dataGridView1.SelectedRows[0].Cells["id"].Value.ToString();
     2                     foreach (Person item in list)
     3                     {
     4                         if(item.id==id)
     5                         {
     6                             list.Remove(item);
     7                             this.dataGridView1.DataSource = new BindingList<Person>(list);
     8                             break;
     9                         }
    10                     }

    改:同样使用foreach遍历集合,找到满足条件的一项进行修改。

     1  for (int i = 0; i < frm.list.Count; i++)
     2                 {
     3                     if (frm.list[i].name == this.name)
     4                     {
     5                         frm.list[i].name = txtname.Text;
     6                         frm.list[i].id = txtid.Text;
     7                         frm.list[i].age = Convert.ToInt32(txtage.Text);
     8                         frm.list[i].sex = this.cobsex.SelectedItem.ToString();
     9                     }
    10                 }

    二:Dictionary<K,V>

    首先确定好Key和Value的数据类型

    增:

    1 HealthCheckSet he = new HealthCheckSet();
    2         this.item.Add(textBox1.Text, he);

    删:

    使用Remove()方法,删除带有特定键的元素。

     1  static void Main(string[] args)
     2         {
     3             //先添加一个元素
     4             //删除一个元素
     5             Dictionary<string, Person> dic = new Dictionary<string, Person>();
     6             Person p1 = new Person();
     7             p1.name = "张三";
     8             p1.age = 19;
     9             dic.Add(p1.name, p1);
    10             dic.Remove(p1.name);
    11             foreach (KeyValuePair<string,Person> item in dic)
    12             {
    13                 Console.WriteLine("key是:{0},Value是:{1}", item.Key, item.Value.age);
    14             }
    15             Console.ReadLine();
    16         }

    改:

     1      static void Main(string[] args)
     2         {
     3              4             //将年龄从19改为20
     5             Dictionary<string, Person> dic = new Dictionary<string, Person>();
     6             Person p1 = new Person();
     7             p1.name = "张三";
     8             p1.age = 19;
     9             dic.Add(p1.name, p1);
    10             foreach (Person item in dic.Values)
    11             {
    12                 if (item.age == 19)
    13                 {
    14                     item.age = 20;
    15                 }
    16                 Console.WriteLine("年龄是:{0}", item.age);
    17             }
    18             Console.ReadLine();
    19         }

    三:泛型类

    1  public class Cat<R>
    2     {
    3         public R Name { get; set; }
    4     }
     1  Cat<string> cat=new Cat<string>();
     2             cat.Name = "2";
     3 
     4 
     5             Cat<int> cat2 = new Cat<int>();
     6             cat2.Name = 12;
     7 
     8             //数组和集合都是    数据类型
     9             Cat<List<string>> cat3 = new Cat<List<string>>();
    10             List<string> list=new List<string>();
    11             cat3.Name =list;
    12 
    13 
    14             Cat<Dictionary<string,string>> cat4=new Cat<Dictionary<string, string>>();
    15             Dictionary<string,string> dic=new Dictionary<string, string>();
    16             cat4.Name = dic;

     

  • 相关阅读:
    CEF调试符号下载地址
    Koa搭建静态文件服务器
    查看requireJS已经加载的模块
    Android Unterminated string at character
    【微信支付】公众号 JSAPI支付 HTML5(使用MUI前端框架)+WebApi 实现流程
    winform 替换word文档中的字段(包含图片添加),生成导出PDF文件(也可是word文件)
    小程序 获取用户信息及手机号码
    winform PictureBox图片上动态添加Label或其他控件
    Html5+Mui前端框架,开发记录(四):下拉菜单绑定数据、搜索、时间控件
    Html5+Mui前端框架,开发记录(三):七牛云 上传图片
  • 原文地址:https://www.cnblogs.com/chimingyang/p/5363730.html
Copyright © 2020-2023  润新知