Skip to content

Commit 468e29c

Browse files
committed
Add CSharp13GrammarExercise
1 parent ae9eb59 commit 468e29c

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace HelloDotNetGuide.CSharp语法
4+
{
5+
public class CSharp13GrammarExercise
6+
{
7+
#region params 集合
8+
9+
public static void SpanDataPrintRun()
10+
{
11+
Span<int> originalSpan = [1, 2, 3, 4, 5];
12+
SpanDataPrint(originalSpan);
13+
}
14+
15+
public static void SpanDataPrint<T>(params Span<T> spans)
16+
{
17+
for (int i = 0; i < spans.Length; i++)
18+
{
19+
Console.WriteLine(spans[i]);
20+
}
21+
}
22+
23+
#endregion
24+
25+
#region 新增Lock锁对象
26+
27+
private object _oldLock = new object();
28+
private System.Threading.Lock _newLock = new System.Threading.Lock();
29+
30+
public void LockTest()
31+
{
32+
lock (_oldLock)
33+
{
34+
Console.WriteLine("Old lock");
35+
}
36+
37+
38+
lock (_newLock)
39+
{
40+
// 传统 lock 语法(优化版)
41+
}
42+
43+
using (_newLock.EnterScope())
44+
{
45+
// 作用域自动释放(推荐写法)
46+
}
47+
48+
_newLock.Enter();
49+
try
50+
{
51+
// 显式 Enter/Exit 调用
52+
}
53+
finally { _newLock.Exit(); }
54+
55+
if (_newLock.TryEnter())
56+
{
57+
try
58+
{
59+
// 非阻塞尝试获取锁
60+
}
61+
finally { _newLock.Exit(); }
62+
}
63+
64+
}
65+
66+
#endregion
67+
68+
#region 新的转义序列
69+
70+
public static void NewEscapeSequence()
71+
{
72+
Console.WriteLine("[31m红色文本[0m");
73+
74+
// C# 13 之前
75+
Console.WriteLine("\u001b[31m红色文本\u001b[0m"); //输出红色文字
76+
77+
// C# 13 中
78+
Console.WriteLine("\e[31m红色文本\e[0m");//功能相同,语法更简洁
79+
}
80+
81+
82+
#endregion
83+
84+
#region 隐式索引访问
85+
86+
public class Numbers
87+
{
88+
public int[] Datas { get; set; } = new int[8];
89+
}
90+
91+
public static void ImplicitIndexAccess()
92+
{
93+
var countdown = new Numbers()
94+
{
95+
Datas =
96+
{
97+
[1] = 0,
98+
[2] = 1,
99+
100+
// 从 C# 13 开始执行下面方式赋值
101+
[^3] = 2,
102+
[^4] = 3,
103+
[^5] = 4
104+
}
105+
};
106+
}
107+
108+
#endregion
109+
110+
#region 重载解析优先级
111+
112+
public class Printer
113+
{
114+
[OverloadResolutionPriority(1)] // 优先调用
115+
public static void Print(params int[] numbers) { }
116+
117+
public static void Print(params ReadOnlySpan<int> numbers) { }
118+
}
119+
120+
#endregion
121+
}
122+
}

‎DotNetGuidePractice/HelloDotNetGuide/Program.cs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ static void Main(string[] args)
1313
{
1414
Console.WriteLine("欢迎来到DotNetGuide练习空间!!!");
1515

16+
#region CSharp13GrammarExercise
17+
18+
CSharp13GrammarExercise.SpanDataPrintRun();
19+
20+
CSharp13GrammarExercise.NewEscapeSequence();
21+
22+
#endregion
23+
1624
#region DictionaryExercise
1725

1826
//DictionaryExercise.DictionaryOperation();
@@ -40,7 +48,7 @@ static void Main(string[] args)
4048
//LinqExercise.CountByExample();
4149
//LinqExercise.AggregateByExample();
4250
//LinqExercise.IndexExample();
43-
LinqExercise.CommonMethodsInLINQ();
51+
//LinqExercise.CommonMethodsInLINQ();
4452

4553
#endregion
4654

0 commit comments

Comments
 (0)