Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(csharp) .NET 8.0 code migration #966

Merged
merged 10 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
dotnet-version: ["6.0.x"]
dotnet-version: ["8.0.x"]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion codes/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN for LANG in $LANGS; do \
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
dpkg -i packages-microsoft-prod.deb && \
apt-get update && \
apt-get install -y dotnet-sdk-6.0 ;; \
apt-get install -y dotnet-sdk-8.0 ;; \
# More languages...
*) \
echo "Warning: No installation workflow for $LANG" ;; \
Expand Down
82 changes: 82 additions & 0 deletions codes/csharp/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,85 @@ csharp_new_line_before_open_brace = none
csharp_new_line_before_else = false
csharp_new_line_before_catch = false
csharp_new_line_before_finally = false
csharp_indent_labels = one_less_than_current
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = true:silent
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent

# CS8981: The type name only contains lower-cased ascii characters. Such names may become reserved for the language.
dotnet_diagnostic.CS8981.severity = silent

# IDE1006: Naming Styles
dotnet_diagnostic.IDE1006.severity = silent

# CA1822: Mark members as static
dotnet_diagnostic.CA1822.severity = silent

[*.{cs,vb}]
#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =

# Naming styles

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
end_of_line = crlf

# IDE0040: Add accessibility modifiers
dotnet_diagnostic.IDE0040.severity = silent

# IDE0044: Add readonly modifier
dotnet_diagnostic.IDE0044.severity = silent
18 changes: 9 additions & 9 deletions codes/csharp/chapter_array_and_linkedlist/array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace hello_algo.chapter_array_and_linkedlist;

public class array {
/* 随机访问元素 */
public static int RandomAccess(int[] nums) {
int RandomAccess(int[] nums) {
Random random = new();
// 在区间 [0, nums.Length) 中随机抽取一个数字
int randomIndex = random.Next(nums.Length);
Expand All @@ -16,7 +16,7 @@ public static int RandomAccess(int[] nums) {
}

/* 扩展数组长度 */
public static int[] Extend(int[] nums, int enlarge) {
int[] Extend(int[] nums, int enlarge) {
// 初始化一个扩展长度后的数组
int[] res = new int[nums.Length + enlarge];
// 将原数组中的所有元素复制到新数组
Expand All @@ -28,7 +28,7 @@ public static int[] Extend(int[] nums, int enlarge) {
}

/* 在数组的索引 index 处插入元素 num */
public static void Insert(int[] nums, int num, int index) {
void Insert(int[] nums, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (int i = nums.Length - 1; i > index; i--) {
nums[i] = nums[i - 1];
Expand All @@ -38,15 +38,15 @@ public static void Insert(int[] nums, int num, int index) {
}

/* 删除索引 index 处元素 */
public static void Remove(int[] nums, int index) {
void Remove(int[] nums, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < nums.Length - 1; i++) {
nums[i] = nums[i + 1];
}
}

/* 遍历数组 */
public static void Traverse(int[] nums) {
void Traverse(int[] nums) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < nums.Length; i++) {
Expand All @@ -59,7 +59,7 @@ public static void Traverse(int[] nums) {
}

/* 在数组中查找指定元素 */
public static int Find(int[] nums, int target) {
int Find(int[] nums, int target) {
for (int i = 0; i < nums.Length; i++) {
if (nums[i] == target)
return i;
Expand All @@ -68,17 +68,17 @@ public static int Find(int[] nums, int target) {
}

/* 辅助函数,数组转字符串 */
public static string ToString(int[] nums) {
string ToString(int[] nums) {
return string.Join(",", nums);
}


[Test]
public static void Test() {
public void Test() {
// 初始化数组
int[] arr = new int[5];
Console.WriteLine("数组 arr = " + ToString(arr));
int[] nums = { 1, 3, 2, 5, 4 };
int[] nums = [1, 3, 2, 5, 4];
Console.WriteLine("数组 nums = " + ToString(nums));

// 随机访问
Expand Down
8 changes: 4 additions & 4 deletions codes/csharp/chapter_array_and_linkedlist/linked_list.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace hello_algo.chapter_array_and_linkedlist;

public class linked_list {
/* 在链表的节点 n0 之后插入节点 P */
public static void Insert(ListNode n0, ListNode P) {
void Insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next;
P.next = n1;
n0.next = P;
}

/* 删除链表的节点 n0 之后的首个节点 */
public static void Remove(ListNode n0) {
void Remove(ListNode n0) {
if (n0.next == null)
return;
// n0 -> P -> n1
Expand All @@ -23,7 +23,7 @@ public static void Remove(ListNode n0) {
}

/* 访问链表中索引为 index 的节点 */
public static ListNode? Access(ListNode head, int index) {
ListNode? Access(ListNode? head, int index) {
for (int i = 0; i < index; i++) {
if (head == null)
return null;
Expand All @@ -33,7 +33,7 @@ public static void Remove(ListNode n0) {
}

/* 在链表中查找值为 target 的首个节点 */
public static int Find(ListNode head, int target) {
int Find(ListNode? head, int target) {
int index = 0;
while (head != null) {
if (head.val == target)
Expand Down
6 changes: 3 additions & 3 deletions codes/csharp/chapter_array_and_linkedlist/list.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class list {
public void Test() {

/* 初始化列表 */
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
List<int> nums = numbers.ToList();
int[] numbers = [1, 3, 2, 5, 4];
List<int> nums = [.. numbers];
Console.WriteLine("列表 nums = " + string.Join(",", nums));

/* 访问元素 */
Expand Down Expand Up @@ -55,7 +55,7 @@ public void Test() {
}

/* 拼接两个列表 */
List<int> nums1 = new() { 6, 8, 7, 10, 9 };
List<int> nums1 = [6, 8, 7, 10, 9];
nums.AddRange(nums1);
Console.WriteLine("将列表 nums1 拼接到 nums 之后,得到 nums = " + string.Join(",", nums));

Expand Down
12 changes: 6 additions & 6 deletions codes/csharp/chapter_backtracking/n_queens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace hello_algo.chapter_backtracking;

public class n_queens {
/* 回溯算法:N 皇后 */
static void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
bool[] cols, bool[] diags1, bool[] diags2) {
// 当放置完所有行时,记录解
if (row == n) {
List<List<string>> copyState = new();
List<List<string>> copyState = [];
foreach (List<string> sRow in state) {
copyState.Add(new List<string>(sRow));
}
Expand All @@ -39,11 +39,11 @@ static void Backtrack(int row, int n, List<List<string>> state, List<List<List<s
}

/* 求解 N 皇后 */
static List<List<List<string>>> NQueens(int n) {
List<List<List<string>>> NQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<string>> state = new();
List<List<string>> state = [];
for (int i = 0; i < n; i++) {
List<string> row = new();
List<string> row = [];
for (int j = 0; j < n; j++) {
row.Add("#");
}
Expand All @@ -52,7 +52,7 @@ static List<List<List<string>>> NQueens(int n) {
bool[] cols = new bool[n]; // 记录列是否有皇后
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后
bool[] diags2 = new bool[2 * n - 1]; // 记录副对角线是否有皇后
List<List<List<string>>> res = new();
List<List<List<string>>> res = [];

Backtrack(0, n, state, res, cols, diags1, diags2);

Expand Down
10 changes: 5 additions & 5 deletions codes/csharp/chapter_backtracking/permutations_i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;

public class permutations_i {
/* 回溯算法:全排列 I */
static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) {
res.Add(new List<int>(state));
Expand All @@ -32,15 +32,15 @@ static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List
}

/* 全排列 I */
static List<List<int>> PermutationsI(int[] nums) {
List<List<int>> res = new();
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
List<List<int>> PermutationsI(int[] nums) {
List<List<int>> res = [];
Backtrack([], nums, new bool[nums.Length], res);
return res;
}

[Test]
public void Test() {
int[] nums = { 1, 2, 3 };
int[] nums = [1, 2, 3];

List<List<int>> res = PermutationsI(nums);

Expand Down
12 changes: 6 additions & 6 deletions codes/csharp/chapter_backtracking/permutations_ii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace hello_algo.chapter_backtracking;

public class permutations_ii {
/* 回溯算法:全排列 II */
static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) {
res.Add(new List<int>(state));
return;
}
// 遍历所有选择
ISet<int> duplicated = new HashSet<int>();
HashSet<int> duplicated = [];
for (int i = 0; i < choices.Length; i++) {
int choice = choices[i];
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
Expand All @@ -34,15 +34,15 @@ static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List
}

/* 全排列 II */
static List<List<int>> PermutationsII(int[] nums) {
List<List<int>> res = new();
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
List<List<int>> PermutationsII(int[] nums) {
List<List<int>> res = [];
Backtrack([], nums, new bool[nums.Length], res);
return res;
}

[Test]
public void Test() {
int[] nums = { 1, 2, 2 };
int[] nums = [1, 2, 2];

List<List<int>> res = PermutationsII(nums);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace hello_algo.chapter_backtracking;

public class preorder_traversal_i_compact {
static List<TreeNode> res;
List<TreeNode> res = [];

/* 前序遍历:例题一 */
static void PreOrder(TreeNode root) {
void PreOrder(TreeNode? root) {
if (root == null) {
return;
}
Expand All @@ -24,12 +24,11 @@ static void PreOrder(TreeNode root) {

[Test]
public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root);

// 前序遍历
res = new List<TreeNode>();
PreOrder(root);

Console.WriteLine("\n输出所有值为 7 的节点");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace hello_algo.chapter_backtracking;

public class preorder_traversal_ii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
List<TreeNode> path = [];
List<List<TreeNode>> res = [];

/* 前序遍历:例题二 */
static void PreOrder(TreeNode root) {
void PreOrder(TreeNode? root) {
if (root == null) {
return;
}
Expand All @@ -29,13 +29,11 @@ static void PreOrder(TreeNode root) {

[Test]
public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root);

// 前序遍历
path = new List<TreeNode>();
res = new List<List<TreeNode>>();
PreOrder(root);

Console.WriteLine("\n输出所有根节点到节点 7 的路径");
Expand Down
Loading