迭代器(iterator)有时又称光标(cursor)是程序设计的软件设计模式
迭代器模式提供一个方法顺序访问一个聚合对象中的各个元素 而又不暴露其内部的标识
在表现效果上看 是可以在容器对象(例如链表或数组)上遍历访问的接口
设计人员无需关心容器对象的内存分配的实现细节
可以用foreach遍历的类,都是实现了迭代器的
关键接口:IEnumeratory,IEnumerable
命名空间:using System.Collections;
可以通过同时继承 IEnumeratory 和 IEnumerable 实现其中的方法
- class CustomList:IEnumerable,IEnumerator
- {
- private int[] list;
- //从-1开始的光标 用于表示 数据得到了哪个位置
- private int position = -1;
- public CustomList()
- {
- list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
- }
-
- #region IEnumerable
- public IEnumerator GetEnumerator()
- {
- Reset();
- return this;
- }
- #endregion
-
- public object Current
- {
- get
- {
- return list[position];
- }
- }
- public bool MoveNext()
- {
- //移动光标
- ++position;
- //是否溢出 溢出就不合法
- return position < list.Length;
- }
- //reset是重置光标位置 一般写在获取
- //用于第一次重置光标位置
- public void Reset()
- {
- position = -1;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("迭代器");
-
- CustomList list = new CustomList();
-
- //foreach本质
- //1.先获取in后面这个对象的 IEnumberator
- // 会调用对象其中的 GetEnumerator 方法来获取
-
- //2.执行得到这个 IEnumerator 对象中的 MoveNext 方法
- //3.只要 MoveNext 方法的返回值是true 就会去得到 Current
- // 然后复制给 item
-
- foreach (int item in list)
- {
- Console.WriteLine(item);
- }
- foreach (int item in list)
- {
- Console.WriteLine(item);
- }
- }
- }
yield return 是C#提供给我们的语法糖
所谓语法糖,也称糖衣语法
主要作用就是将复杂逻辑简单化,可以增加程序的可读性 从而减少程序代码出错的机会
关键接口:IEnumerable
命名空间:using System.Collections;
让想要通过foreach遍历的自定义类实现接口中的方法GetEnumerator即可
- class CustomList2 : IEnumerable
- {
- private int[] list;
- public CustomList2()
- {
- list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
- }
- public IEnumerator GetEnumerator()
- {
- for (int i = 0; i < list.Length; i++)
- {
- //yield关键字 配合迭代器使用
- //可以理解为 暂时返回 保留当前的状态
- //一会儿还会再回来
- //C#的语法糖
- yield return list[i];
- }
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("迭代器");
-
- CustomList2 list2 = new CustomList2();
- foreach (int item in list2)
- {
- Console.WriteLine(item);
- }
- foreach (int item in list2)
- {
- Console.WriteLine(item);
- }
-
- }
- }
- class CustomList<T> : IEnumerable
- {
- private T[] array;
- public CustomList(params T[] array)
- {
- this.array = array;
- }
- public IEnumerator GetEnumerator()
- {
- for (int i = 0; i < array.Length; i++)
- {
- yield return array[i];
- }
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("迭代器");
-
- CustomList<string> list3 = new CustomList<string>("123", "321", "333", "555");
- foreach (string item in list3)
- {
- Console.WriteLine(item);
- }
- }
- }
迭代器就是可以让我们在外部直接通过 foreach 遍历对象中元素而不需要了解其结构
主要的两种方式
1.传统方式 继承两个接口 实现里面的方法
2.用语法糖 yield return 去返回内容 只需要继承一个接口即可
- class Person
- {
- private int money;
- public bool sex;
- public string Name
- {
- get => "张三";
- set => sex = true;
- }
- public int Age
- {
- get;
- set;
- }
- public Person(int money)
- {
- this.money = money;
- }
- public int Add(int x, int y) => x + y;
-
- public void Speak(string str) => Console.WriteLine(str);
- }
var 是一种特殊的变量类型
它可以用来表示任意类型的变量
注意:
1.var 不能作为类的成员 只能用于临时变量申明时 也就是 一般写在函数语句块中
2.var 必须初始化
- var i = 5;
- var s = "123";
- var array = new int[] { 1, 2, 3, 4, };
- var list = new List<int>();
- //在类型不确定的情况下可以用 var 缺点 看代码时需要看后面代码才能确定var代表的类型
申明对象时 可以通过直接写大括号的形式初始化公共成员变量和属性
- Person p = new Person (100){ sex=true,Age=18,Name="张三"};
- Person p2 = new Person (200){ Age = 18 };
申明集合对象时 也可以通过大括号 直接初始化内部属性
- int[] array2 = new int[] { 1, 2, 3, 4, 5 };
-
- List<int> listInt = new List<int>() { 1, 2, 3, 4, 5 };
-
- List
listPerson = new List() { - new Person(100),
- new Person(100) { Age=10},new Person(1){ sex=true,Name="张三"}
- };
-
- Dictionary<int, string> dic = new Dictionary<int, string>()
- {
- { 1,"123"},
- { 2,"222"}
- };
var 变量可以申明为自定义的匿名类型
- var v = new { age = 10, money = 11, name = "小明" };
- Console.WriteLine(v.age);
- Console.WriteLine(v.money);
- Console.WriteLine(v.name);
- //1.值类型是不能赋值为 空的
- //int c = null;
-
- //2.申明时 在值类型后面加?可以赋值为空
- int? c = null;
-
- //3.判断是否为空
- if (c.HasValue)
- {
- Console.WriteLine(c);
- Console.WriteLine(c.Value);
- }
-
- //4.安全获取可空类型值
- int? value = null;
- // 4-1.如果为空 默认返回值类型的默认值
- Console.WriteLine(value.GetValueOrDefault());
- // 4-2.也可以指定一个默认值
- Console.WriteLine(value.GetValueOrDefault(100));
- Console.WriteLine(value);
-
- float? f = null;
- double? d = null;
-
- object o = null;
- if (o != null)
- {
- o.ToString();
- }
- //相当于是一种语法糖 能够帮助我们自动去判断是否为空
- //如果是null 就不会执行toString 也不会报错
- Console.WriteLine(o?.ToString());
-
- int[] arrayInt = null;
- //Console.WriteLine(arrayInt[0]);//会报错
- Console.WriteLine(arrayInt?[0]);
-
- Action action = null;
- //if (action != null)
- //{
- // action();
- //}
- action?.Invoke();
空合并操作符 ??
左边值 ?? 右边值
如果左边值为null 就返回右边值 否则返回左边值
只要是可以为null的类型都能用
- int? intV = null;
- //int intI = intV == null ? 100 : intV.Value;
- //简写
- int intI = intV ?? 100;
- Console.WriteLine(intI);
-
- string str = null;
- str = str ?? "haha";
- Console.WriteLine(str);
关键符号:$
用$来构造字符串,让字符串中可以拼接变量
- string name = "张三";
- int age = 18;
- Console.WriteLine($"好好学习,{name},年龄:{age}");
当循环或者if语句中只有 一句代码时 大括号可以省略
- if (true)
- Console.WriteLine("123123");
-
- for (int j = 0; j < 10; j++)
- Console.WriteLine(j);