|
| 1 | +using PointTest = (int item1, int item2); |
| 2 | + |
| 3 | +namespace HelloDotNetGuide.CSharp语法 |
| 4 | +{ |
| 5 | + public class CSharp12GrammarExercise |
| 6 | + { |
| 7 | + public static void OutputPrint() |
| 8 | + { |
| 9 | + //使用 using 关键字为元组类型创建别名,并进行调用: |
| 10 | + PointTest point = (10, 20); |
| 11 | + Console.WriteLine($"输出:Item1={point.Item1}, Item2={point.Item2}"); |
| 12 | + |
| 13 | + //InlineArrays(); |
| 14 | + //CollectionExpressions(); |
| 15 | + //var person = new Person("追逐时光者", 30); |
| 16 | + //Console.WriteLine($"{person.Name}, {person.Age}"); |
| 17 | + |
| 18 | + //// 创建 Distance 结构体实例 |
| 19 | + //Distance distance = new Distance(10, 55); |
| 20 | + //// 访问 Magnitude 和 Direction 属性 |
| 21 | + //Console.WriteLine($"Magnitude: {distance.Magnitude},Direction: {distance.Direction}"); |
| 22 | + } |
| 23 | + |
| 24 | + #region 默认 lambda 参数 |
| 25 | + |
| 26 | + public static void DefaultLambdaParameters() |
| 27 | + { |
| 28 | + Func<double, double> testcube = x => x * x * x; |
| 29 | + Func<int, int, bool> testForEquality = (x, y) => x == y; |
| 30 | + Func<int, string, bool> isTooLong = (int x, string s) => s.Length > x; |
| 31 | + } |
| 32 | + |
| 33 | + #endregion |
| 34 | + |
| 35 | + #region 集合表达式 |
| 36 | + |
| 37 | + public static void CollectionExpressions() |
| 38 | + { |
| 39 | + // 创建一个数组 |
| 40 | + int[] array = [55, 99, 100, 33]; |
| 41 | + |
| 42 | + // 创建一个列表 |
| 43 | + List<string> list = ["one", "two", "three", "five", "追逐时光者"]; |
| 44 | + |
| 45 | + // 创建一个 Span |
| 46 | + Span<char> span = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'k']; |
| 47 | + |
| 48 | + // 创建一个交错二维数组 |
| 49 | + int[][] two2D = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [88, 8, 9]]; |
| 50 | + |
| 51 | + int[] item0 = [88, 2, 3]; |
| 52 | + int[] item1 = [22, 5, 6]; |
| 53 | + int[] item2 = [7, 99, 9]; |
| 54 | + int[] totalList = [.. item0, .. item1, .. item2]; |
| 55 | + foreach (var element in totalList) |
| 56 | + { |
| 57 | + Console.Write($"{element}, "); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + #endregion |
| 62 | + |
| 63 | + #region 内联数组 |
| 64 | + |
| 65 | + public static void InlineArrays() |
| 66 | + { |
| 67 | + var buffer = new Buffer(); |
| 68 | + for (int i = 0; i < 20; i++) |
| 69 | + { |
| 70 | + buffer[i] = i; |
| 71 | + } |
| 72 | + |
| 73 | + foreach (var i in buffer) |
| 74 | + { |
| 75 | + Console.WriteLine(i); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + #endregion |
| 80 | + } |
| 81 | + |
| 82 | + #region 主构造函数 |
| 83 | + |
| 84 | + public readonly struct Distance(double dx, double dy) |
| 85 | + { |
| 86 | + public readonly double Magnitude { get; } = Math.Sqrt(dx * dx + dy * dy); |
| 87 | + public readonly double Direction { get; } = Math.Atan2(dy, dx); |
| 88 | + } |
| 89 | + |
| 90 | + public class Person(string name, int age) |
| 91 | + { |
| 92 | + public string Name => name; |
| 93 | + public int Age => age; |
| 94 | + } |
| 95 | + |
| 96 | + #endregion |
| 97 | + |
| 98 | + [System.Runtime.CompilerServices.InlineArray(20)] |
| 99 | + public struct Buffer |
| 100 | + { |
| 101 | + private int _element0; |
| 102 | + } |
| 103 | +} |
0 commit comments