doc: update docs/cs.md (#608)

This commit is contained in:
jaywcjlove 2024-04-11 17:57:47 +08:00
parent f682690b9f
commit 51336f8b86

View File

@ -961,6 +961,7 @@ bool containsCherries = words.Contains("cherries");
<!--rehype:className=wrap-text-->
### ConcurrentBag
<!--rehype:wrap-class=col-span-2-->
```cs
// 创建一个并发安全的整数集合
@ -975,39 +976,14 @@ foreach (var number in concurrentNumbers.ToArray())
{
concurrentNumbers.TryTake(out _number); // 并发安全地移除一个元素
}
// 修改(无法直接修改,同样需先移除再添加,但由于并发特性,不能保证一定能修改目标元素)
// 在并发环境下尤其复杂,此处省略示例
// 查询Contains
bool hasOne = concurrentNumbers.Contains(1);
```
### Dictionary
修改(无法直接修改,同样需先移除再添加,但由于并发特性,不能保证一定能修改目标元素)。
在并发环境下尤其复杂,此处省略示例
```cs
// 创建一个键值对字典
Dictionary<string, int> scores = new Dictionary<string, int>
{
{ "Alice", 85 },
{ "Bob", 90 }
};
// 增加Add
scores.Add("Charlie", 88);
// 删除Remove
scores.Remove("Bob");
// 修改Update
if (scores.ContainsKey("Alice"))
{
scores["Alice"] = 90; // 直接替换值
}
// 查询ContainsKey / GetValueOrDefault
bool aliceExists = scores.ContainsKey("Alice");
int charlieScore = scores.GetValueOrDefault("Charlie", 0);
// 查询Contains
bool hasOne = concurrentNumbers.Contains(1);
```
### Stack
@ -1034,6 +1010,34 @@ int peekedValue = stack.Peek();
bool hasTwo = stack.Contains(2);
```
### Dictionary
<!--rehype:wrap-class=col-span-2-->
```cs
// 创建一个键值对字典
Dictionary<string, int> scores = new Dictionary<string, int>
{
{ "Alice", 85 },
{ "Bob", 90 }
};
// 增加Add
scores.Add("Charlie", 88);
// 删除Remove
scores.Remove("Bob");
// 修改Update
if (scores.ContainsKey("Alice"))
{
scores["Alice"] = 90; // 直接替换值
}
// 查询ContainsKey / GetValueOrDefault
bool aliceExists = scores.ContainsKey("Alice");
int charlieScore = scores.GetValueOrDefault("Charlie", 0);
```
### Hashtable
```cs
@ -1061,17 +1065,12 @@ bool hasKey2 = hashTable.ContainsKey("key2");
string valueOfKey2 = (string)hashTable["key2"];
```
语法糖
-----------
----
> 语法糖需要根据 `c#` 版本来确实是否可以使用,一般情况下 `c# 8.0` 及以上的 `C#` 版本都已支持。
### 对象判空及赋值
<!--rehype:wrap-class=col-span-2-->
```cs
@ -1092,7 +1091,6 @@ if(obj == null)
obj ??= new object();
```
### 可空类型判空及赋值
```cs
@ -1113,37 +1111,39 @@ else
int result = nums ?? -1;
```
### 减少空引用
判断数组或 `list` 不能 `null` 且有元素
```cs
// 判断数组或list不能null且有元素
if(list != null && list.Count > 0)
```
// 简化的语法糖 当list为null时将直接返回false
简化的语法糖当 `list``null` 时,将直接返回 `false`
```cs
if(list?.Count > 0)
```
// 同样可运用在赋值时如果obj为null将不会取obj.text的值而是将会为text赋值null
同样可运用在赋值时,如果 `obj``null`,将不会取 `obj.text` 的值,而是将会为 `text` 赋值 `null`
```cs
string text = obj?.text;
```
<!--rehype:className=wrap-text-->
### 判断参数类型并转换类型+校验
- 判断 `value` 是否为 `string` 类型,如果 `value``string` 类型
- 那么将 `value` 转换为 `string` 类型,并赋值给 `stringValue`
- 再判断 `stringValue` 是否不为 `Null``空`
<!--rehype:className=style-timeline-->
```cs
// 1.判断value是否为 string 类型如果value是 string 类型
// 2.那么将value转换为 string 类型,并赋值给 stringValue
// 3.再判断 stringValue是否不为Null或空
if(value is string stringValue && !string.IsNullOrEmpty(stringValue))
```
<!--rehype:className=wrap-text-->
### Switch
```cs
@ -1163,8 +1163,6 @@ public string GetNums(int num)
}
```
### 切片操作
<!--rehype:wrap-class=col-span-2-->
@ -1198,7 +1196,6 @@ string[] strs3 = arr[3..7];
string[] strs4 = arr[^4..^2];
```
杂项
-----------
@ -1211,4 +1208,4 @@ string[] strs4 = arr[^4..^2];
`Common Language Runtime (CLR)` | 通用语言运行库 | 主要定位、加载和托管 .NET 对象。<br/>CLR 还处理内存管理、应用程序托管、线程协调、执行安全检查和其他低级细节
`Managed code` | 托管代码 | 在 `.NET` 运行时编译和运行的代码。 C#/F#/VB 就是例子
`Unmanaged code` | 非托管代码 | 直接编译为机器代码且不能由 .NET 运行时直接托管的代码。<br/>不包含空闲内存管理、垃圾收集等。从 C/C++ 创建的 DLL 就是示例
<!--rehype:className=show-header-->
<!--rehype:className=show-header left-align-->