Skip to content

Commit

Permalink
doc: update docs/cs.md (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 11, 2024
1 parent f682690 commit 51336f8
Showing 1 changed file with 62 additions and 65 deletions.
127 changes: 62 additions & 65 deletions docs/cs.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ bool containsCherries = words.Contains("cherries");
<!--rehype:className=wrap-text-->

### ConcurrentBag
<!--rehype:wrap-class=col-span-2-->

```cs
// 创建一个并发安全的整数集合
Expand All @@ -975,15 +976,42 @@ foreach (var number in concurrentNumbers.ToArray())
{
concurrentNumbers.TryTake(out _number); // 并发安全地移除一个元素
}
```

// 修改(无法直接修改,同样需先移除再添加,但由于并发特性,不能保证一定能修改目标元素)
// 在并发环境下尤其复杂,此处省略示例
修改(无法直接修改,同样需先移除再添加,但由于并发特性,不能保证一定能修改目标元素)
在并发环境下尤其复杂,此处省略示例

```cs
// 查询(Contains)
bool hasOne = concurrentNumbers.Contains(1);
```

### Stack

```cs
// 创建一个整数栈
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);

// 增加(Push)
stack.Push(3);

// 删除(Pop)并返回栈顶元素
int topNumber = stack.Pop();

// 修改(Stack不支持直接修改元素,需先Pop再Push)
int poppedValue = stack.Pop();
// 替换刚弹出的值
stack.Push(poppedValue * 2);

// 查询(Peek / Contains) 但不移除栈顶元素
int peekedValue = stack.Peek();
bool hasTwo = stack.Contains(2);
```

### Dictionary
<!--rehype:wrap-class=col-span-2-->

```cs
// 创建一个键值对字典
Expand All @@ -1010,30 +1038,6 @@ bool aliceExists = scores.ContainsKey("Alice");
int charlieScore = scores.GetValueOrDefault("Charlie", 0);
```

### Stack

```cs
// 创建一个整数栈
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);

// 增加(Push)
stack.Push(3);

// 删除(Pop)并返回栈顶元素
int topNumber = stack.Pop();

// 修改(Stack不支持直接修改元素,需先Pop再Push)
int poppedValue = stack.Pop();
// 替换刚弹出的值
stack.Push(poppedValue * 2);

// 查询(Peek / Contains) 但不移除栈顶元素
int peekedValue = stack.Peek();
bool hasTwo = stack.Contains(2);
```

### Hashtable

```cs
Expand Down Expand Up @@ -1061,17 +1065,12 @@ bool hasKey2 = hashTable.ContainsKey("key2");
string valueOfKey2 = (string)hashTable["key2"];
```



语法糖
-----------

> 语法糖需要根据`c#`版本来确实是否可以使用,一般情况下`c# 8.0`及以上的`C#`版本都已支持。
----

> 语法糖需要根据 `c#` 版本来确实是否可以使用,一般情况下 `c# 8.0` 及以上的 `C#` 版本都已支持。
### 对象判空及赋值

<!--rehype:wrap-class=col-span-2-->

```cs
Expand All @@ -1085,14 +1084,13 @@ obj ?? throw new NullReferenceException();
// 对象不为空 不进行赋值
if(obj == null)
{
obj = new object();
obj = new object();
}

// 简化的语法糖
obj ??= new object();
```


### 可空类型判空及赋值

```cs
Expand All @@ -1102,69 +1100,69 @@ int? nums = null;
// 判断值是否为空,并进行不同的赋值
if(nums == null)
{
result = -1;
result = -1;
}
else
{
result = nums;
result = nums;
}

// 简化的语法糖
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-->


### 判断参数类型并转换类型+校验

```cs
// 1.判断value是否为 string 类型,如果value是 string 类型
// 2.那么将value转换为 string 类型,并赋值给 stringValue
// 3.再判断 stringValue是否不为Null或空
- 判断 `value` 是否为 `string` 类型,如果 `value``string` 类型
- 那么将 `value` 转换为 `string` 类型,并赋值给 `stringValue`
- 再判断 `stringValue` 是否不为 `Null```
<!--rehype:className=style-timeline-->

```cs
if(value is string stringValue && !string.IsNullOrEmpty(stringValue))
```
<!--rehype:className=wrap-text-->


### Switch

```cs
public string GetNums(int num)
{
// 使用这种方式的switch时,要求返回类型统一
string str = num switch
{
1 => "num的值是1",
2 => "num的值是2",
3 => "num的值是3",
4 => "num的值是4",
_ => "其他"
};

return str;
// 使用这种方式的switch时,要求返回类型统一
string str = num switch
{
1 => "num的值是1",
2 => "num的值是2",
3 => "num的值是3",
4 => "num的值是4",
_ => "其他"
};

return str;
}
```



### 切片操作

<!--rehype:wrap-class=col-span-2-->
Expand Down Expand Up @@ -1198,7 +1196,6 @@ string[] strs3 = arr[3..7];
string[] strs4 = arr[^4..^2];
```


杂项
-----------

Expand All @@ -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-->

0 comments on commit 51336f8

Please sign in to comment.