一、抽象类与抽象方法1.1 什么是抽象类抽象类是一种不能被实例化的类它用于定义通用行为并强制子类实现某些方法。csharp// 抽象类定义“动物”的通用特征 public abstract class Animal { public string Name { get; set; } // 抽象方法没有方法体子类必须实现 public abstract void MakeSound(); // 普通方法子类可以直接使用 public void Eat() { Console.WriteLine(${Name} 正在吃东西); } }1.2 子类实现抽象类csharppublic class Dog : Animal { public override void MakeSound() { Console.WriteLine(${Name} 汪汪叫); } } public class Cat : Animal { public override void MakeSound() { Console.WriteLine(${Name} 喵喵叫); } }二、多态同一个接口不同实现多态允许我们用父类类型的变量引用子类对象调用方法时执行的是子类的实现。csharpAnimal a1 new Dog { Name 旺财 }; Animal a2 new Cat { Name 咪咪 }; a1.MakeSound(); // 输出旺财 汪汪叫 a2.MakeSound(); // 输出咪咪 喵喵叫好处提高代码的扩展性和可维护性。三、集合动态管理数据数组长度固定而集合可以动态增删元素。3.1ListT– 最常用的泛型集合csharpListstring students new Liststring(); students.Add(张三); students.Add(李四); students.Remove(张三); foreach (var s in students) { Console.WriteLine(s); }3.2DictionaryTKey, TValue– 键值对集合csharpDictionarystring, int scores new Dictionarystring, int(); scores.Add(张三, 90); scores.Add(李四, 85); int zhangScore scores[张三]; // 通过键取值 Console.WriteLine($张三的成绩{zhangScore});四、泛型类型参数化泛型让你编写一份代码适用于多种数据类型。4.1 泛型方法示例csharppublic T GetMaxT(T a, T b) where T : IComparable { return a.CompareTo(b) 0 ? a : b; } // 使用 int maxInt GetMax(10, 20); // 20 string maxStr GetMax(apple, banana); // banana4.2 泛型类示例csharppublic class BoxT { public T Value { get; set; } } Boxint intBox new Boxint { Value 100 }; Boxstring strBox new Boxstring { Value Hello;五、异常处理让程序更健壮使用try-catch-finally捕获运行时错误避免程序崩溃。csharptry { int[] nums { 1, 2, 3 }; Console.WriteLine(nums[5]); // 越界错误 } catch (IndexOutOfRangeException ex) { Console.WriteLine(数组越界 ex.Message); } catch (Exception ex) // 捕获所有其他异常 { Console.WriteLine(发生错误 ex.Message); } finally {