|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace ComprehensiveCSharpExample |
| 6 | +{ |
| 7 | + // 1. 枚举 |
| 8 | + enum Direction { North, South, East, West } |
| 9 | + |
| 10 | + // 2. 接口 |
| 11 | + interface IDrawable |
| 12 | + { |
| 13 | + void Draw(); |
| 14 | + } |
| 15 | + |
| 16 | + // 3. 结构体 |
| 17 | + struct Point |
| 18 | + { |
| 19 | + public int X { get; } |
| 20 | + public int Y { get; } |
| 21 | + |
| 22 | + public Point(int x, int y) |
| 23 | + { |
| 24 | + X = x; |
| 25 | + Y = y; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // 4. 类与继承、多态 |
| 30 | + class Shape |
| 31 | + { |
| 32 | + public virtual double Area() => 0.0; |
| 33 | + } |
| 34 | + |
| 35 | + class Circle : Shape |
| 36 | + { |
| 37 | + public double Radius { get; } |
| 38 | + |
| 39 | + public Circle(double radius) |
| 40 | + { |
| 41 | + Radius = radius; |
| 42 | + } |
| 43 | + |
| 44 | + public override double Area() => Math.PI * Radius * Radius; |
| 45 | + } |
| 46 | + |
| 47 | + class Square : Shape, IDrawable |
| 48 | + { |
| 49 | + public double Side { get; } |
| 50 | + |
| 51 | + public Square(double side) |
| 52 | + { |
| 53 | + Side = side; |
| 54 | + } |
| 55 | + |
| 56 | + public override double Area() => Side * Side; |
| 57 | + |
| 58 | + public void Draw() |
| 59 | + { |
| 60 | + Console.WriteLine($"Drawing a square with side {Side}"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // 5. 泛型类 |
| 65 | + class Box<T> |
| 66 | + { |
| 67 | + public T Value { get; } |
| 68 | + |
| 69 | + public Box(T value) |
| 70 | + { |
| 71 | + Value = value; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + class Program |
| 76 | + { |
| 77 | + // 6. 委托与事件 |
| 78 | + public delegate void Notify(string message); |
| 79 | + public static event Notify OnNotify; |
| 80 | + |
| 81 | + static void Main(string[] args) |
| 82 | + { |
| 83 | + // 7. 变量与数据类型 |
| 84 | + int number = 42; |
| 85 | + double pi = 3.14159; |
| 86 | + bool isTrue = true; |
| 87 | + char letter = 'C'; |
| 88 | + string text = "Hello, C#!"; |
| 89 | + |
| 90 | + Console.WriteLine($"Number: {number}, Pi: {pi}, IsTrue: {isTrue}, Letter: {letter}, Text: {text}"); |
| 91 | + |
| 92 | + // 8. 控制流 |
| 93 | + if (number > 40) |
| 94 | + { |
| 95 | + Console.WriteLine("Number is greater than 40"); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + Console.WriteLine("Number is 40 or less"); |
| 100 | + } |
| 101 | + |
| 102 | + for (int i = 0; i < 5; i++) |
| 103 | + { |
| 104 | + Console.WriteLine($"i = {i}"); |
| 105 | + } |
| 106 | + |
| 107 | + int counter = 0; |
| 108 | + while (counter < 5) |
| 109 | + { |
| 110 | + Console.WriteLine($"Counter = {counter}"); |
| 111 | + counter++; |
| 112 | + } |
| 113 | + |
| 114 | + // 9. 函数调用 |
| 115 | + int sum = Add(10, 20); |
| 116 | + Console.WriteLine($"Sum: {sum}"); |
| 117 | + |
| 118 | + // 10. 枚举使用 |
| 119 | + Navigate(Direction.East); |
| 120 | + |
| 121 | + // 11. 类与对象 |
| 122 | + Circle circle = new Circle(5.0); |
| 123 | + Square square = new Square(4.0); |
| 124 | + Console.WriteLine($"Circle Area: {circle.Area()}"); |
| 125 | + Console.WriteLine($"Square Area: {square.Area()}"); |
| 126 | + square.Draw(); |
| 127 | + |
| 128 | + // 12. 结构体 |
| 129 | + Point point = new Point(10, 20); |
| 130 | + Console.WriteLine($"Point coordinates: X = {point.X}, Y = {point.Y}"); |
| 131 | + |
| 132 | + // 13. 泛型 |
| 133 | + Box<int> intBox = new Box<int>(123); |
| 134 | + Box<string> stringBox = new Box<string>("Generics in C#"); |
| 135 | + Console.WriteLine($"Int Box: {intBox.Value}"); |
| 136 | + Console.WriteLine($"String Box: {stringBox.Value}"); |
| 137 | + |
| 138 | + // 14. 集合与 LINQ |
| 139 | + List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; |
| 140 | + var doubled = numbers.Select(n => n * 2); |
| 141 | + var filtered = numbers.Where(n => n > 3); |
| 142 | + |
| 143 | + Console.WriteLine("Doubled numbers: " + string.Join(", ", doubled)); |
| 144 | + Console.WriteLine("Filtered numbers: " + string.Join(", ", filtered)); |
| 145 | + |
| 146 | + // 15. 异常处理 |
| 147 | + try |
| 148 | + { |
| 149 | + int result = Divide(10, 0); |
| 150 | + Console.WriteLine($"Result: {result}"); |
| 151 | + } |
| 152 | + catch (DivideByZeroException e) |
| 153 | + { |
| 154 | + Console.WriteLine("Error: " + e.Message); |
| 155 | + } |
| 156 | + |
| 157 | + // 16. 事件触发 |
| 158 | + OnNotify += MessageHandler; |
| 159 | + OnNotify?.Invoke("This is a notification message."); |
| 160 | + } |
| 161 | + |
| 162 | + // 17. 函数 |
| 163 | + static int Add(int a, int b) => a + b; |
| 164 | + |
| 165 | + // 18. 异常处理函数 |
| 166 | + static int Divide(int a, int b) |
| 167 | + { |
| 168 | + if (b == 0) throw new DivideByZeroException("Cannot divide by zero"); |
| 169 | + return a / b; |
| 170 | + } |
| 171 | + |
| 172 | + // 19. 枚举与 switch 语句 |
| 173 | + static void Navigate(Direction direction) |
| 174 | + { |
| 175 | + switch (direction) |
| 176 | + { |
| 177 | + case Direction.North: |
| 178 | + Console.WriteLine("Going North"); |
| 179 | + break; |
| 180 | + case Direction.South: |
| 181 | + Console.WriteLine("Going South"); |
| 182 | + break; |
| 183 | + case Direction.East: |
| 184 | + Console.WriteLine("Going East"); |
| 185 | + break; |
| 186 | + case Direction.West: |
| 187 | + Console.WriteLine("Going West"); |
| 188 | + break; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // 20. 事件处理函数 |
| 193 | + static void MessageHandler(string message) |
| 194 | + { |
| 195 | + Console.WriteLine($"Received message: {message}"); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments