C#学习笔记20240705

List集合

List集合的声明
  • List集合与ArrayList集合都继承了相同的接口,故使用与ArrayList相似。
  • 在声明List集合时,需要同时为其声明List集合内数据的对象类型。
  • 示例:
List <int> intList = new List <int>()

所谓接口目前可以简单理解为限制和规定类型行为即类型方法的一种手段。

            List <int> intList=new List<int>()
            {
                1,2,3,4,5
            };
            intList.Add(1);
            //intList.Add("abc");//定义了int类型的泛型限制,List集合无法存放其他类型
            intList[0] = 2;
            intList.Insert(0, 3);
            intList.RemoveAt(0);
            intList.Remove(2);
            intList.Clear();    
自己创建类型限定List集合数据类型

定义Person1类型

 public class Person1
    {
        public int Age { get; set; }
        public int Height { get; set; }
        public string Name { get; set; }
    }
 public static void P43集合字典List()
        {
            List<Person1> people = new List<Person1>();
            Person1 person1 = new Person1
            {
                Age = 18,
                Height = 175,
                Name = "张三"
            };
            people.Add(person1);
            Person1 person2 = people[0];
            //people.RemoveAt[0];
            people.Remove(person1);
        }

此时可以成功删除

public static void P43集合字典List()
        {
            List<Person1> people = new List<Person1>();
            Person1 person1 = new Person1
            {
                Age = 18,
                Height = 175,
                Name = "张三"
            };
            people.Add(new Person1
            {
                Age = 18,
                Height = 175,
                Name = "张三"
            });
            Person1 person2 = people[0];
            //people.RemoveAt[0];
            people.Remove(person1);
        }

此时删除失败

 public static void P43集合字典List()
        {
            List<Person1> people = new List<Person1>();
            Person1 person1 = new Person1
            {
                Age = 18,
                Height = 175,
                Name = "张三"
            };
            people.Add(new Person1
            {
                Age = 18,
                Height = 175,
                Name = "张三"
            });
            Person1 person2 = people[0];
            //people.RemoveAt[0];
            people.Remove(person2);
        }

此时可以成功删除。
原因:Remove认的也是地址,第二种情况删除失败,people.Add(new Person1和person1不是同一个地址,每次new都会开辟出一个新的地址。

总结
  • 集合与数组比较类似,都用于存放一组值
  • 集合中提供了特定的方法能直接操作集合中的数据,并提供了不同的集合类型来实现特定的功能
  • 简单地说就是数组的升级版,可以动态的对集合的长度进行定义和维护
  • List泛型的好处指通过允许指定泛型类或方法操作的特定类型,减少了类型强制转换的需要和运行时错误的可能性,泛型提供了类型安全,但没有增加开销。

字典

Dictionary声明
  • 在声明Dictionary字典时,需要同时为其声明Dictionary字典内键和值的类型
  • 示例: Dictionary<int,string> dictionary= new Dictionary<int,string>();
  • 键和值可以是任何类型,但是键必须在设置时是唯一的,而值可以不唯一。
public static void P44字典()
        {
            Dictionary<int,string> dictionary= new Dictionary<int,string>();
            dictionary.Add(1, "98分");
            dictionary.Add(2, "92分");
            dictionary.Add(3, "89分");
            //dictionary.Add(1, "88分");//系统会报错
            //方法二:索引器赋值
            dictionary[1] = "88分";//系统不会报错
            dictionary[4] = "99分";
            //方法三:对象初始化
            Dictionary<string, string> dictionary2 = new Dictionary<string, string>()
            {
                {"A","aa" },
                {"B","bb" },
                {"C","cc" },
            };
            //获取键为1的值
            string value = dictionary[1];
            //移除键为1的键值对
            dictionary.Remove(1);
        }
foreach遍历(增强for循环)
public static void P45foreach遍历()
        {
            int[] ints = { 1, 2, 3, };
            foreach(int item in ints)
            {
                Console.Write(item.ToString());
            }
            List<int> intList= new List<int>() { 1, 2, 3 };
            foreach(int item in intList)
            {
                Console.Write(item.ToString());
            }
            Dictionary<string, string> dictionary = new Dictionary<string, string>()
            {
                 {"A","aa" },
                {"B","bb" },
                {"C","cc" },
            };
            foreach(KeyValuePair<string,string>item in dictionary)
            {
                string key=item.Key;
                string value=item.Value;
            }

数据库

约束
  1. 唯一约束(unique):唯一,不允许存在两行数据,在这个指定列上重复.。此处的不能重复指的是指定列不同行之间。
  2. 主键约束(primary key):就是一条数据的身份标识,有助于更容易跟快速的找到表中的一个特定记录。主键相当于是 not null 和 unique 的结合,通过 primary key 这个约束指定某一列为主键。
  3. 外键约束(foreign key):两个表之间的数据建立的连接,一个表可以有一个或多个外键。外键用于定义父表和子表之间的关系,外键定义在子表上,父表必须有主键约束或unique约束。
  4. 限制约束(check):CHECK 约束用于限制列中的值的范围。如果对单个列定义 CHECK 约束,那么该列只允许特定的值。如果对一个表定义 CHECK 约束,那么此约束会基于行中其他列的值在特定的列中对值进行限制。
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值