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 4 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
73 changes: 73 additions & 0 deletions codes/csharp/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,76 @@ 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

[*.{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
2 changes: 1 addition & 1 deletion codes/csharp/chapter_array_and_linkedlist/array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static 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
4 changes: 2 additions & 2 deletions codes/csharp/chapter_array_and_linkedlist/linked_list.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void Remove(ListNode n0) {
}

/* 访问链表中索引为 index 的节点 */
public static ListNode? Access(ListNode head, int index) {
public static 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) {
public static 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
8 changes: 4 additions & 4 deletions codes/csharp/chapter_backtracking/n_queens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void Backtrack(int row, int n, List<List<string>> state, List<List<List<s
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 Down Expand Up @@ -41,9 +41,9 @@ static void Backtrack(int row, int n, List<List<string>> state, List<List<List<s
/* 求解 N 皇后 */
static 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
6 changes: 3 additions & 3 deletions codes/csharp/chapter_backtracking/permutations_i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ 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>> 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
8 changes: 4 additions & 4 deletions codes/csharp/chapter_backtracking/permutations_ii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List
return;
}
// 遍历所有选择
ISet<int> duplicated = new HashSet<int>();
HashSet<int> duplicated = [];
for (int i = 0; i < choices.Length; i++) {
int choice = choices[i];
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
Expand All @@ -35,14 +35,14 @@ 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>> 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;
static readonly List<TreeNode> res = [];

/* 前序遍历:例题一 */
static void PreOrder(TreeNode root) {
static 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;
static readonly List<TreeNode> path = [];
static readonly List<List<TreeNode>> res = [];

/* 前序遍历:例题二 */
static void PreOrder(TreeNode root) {
static 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
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_iii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
static readonly List<TreeNode> path = [];
static readonly List<List<TreeNode>> res = [];

/* 前序遍历:例题三 */
static void PreOrder(TreeNode root) {
static void PreOrder(TreeNode? root) {
// 剪枝
if (root == null || root.val == 3) {
return;
Expand All @@ -30,13 +30,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 的路径,路径中不包含值为 3 的节点");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<Tr
// 尝试:做出选择,更新状态
MakeChoice(state, choice);
// 进行下一轮选择
Backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
Backtrack(state, [choice.left!, choice.right!], res);
// 回退:撤销选择,恢复到之前的状态
UndoChoice(state, choice);
}
Expand All @@ -55,14 +55,14 @@ static void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<Tr

[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);

// 回溯算法
List<List<TreeNode>> res = new();
List<TreeNode> choices = new() { root };
Backtrack(new List<TreeNode>(), choices, res);
List<List<TreeNode>> res = [];
List<TreeNode> choices = [root!];
Backtrack([], choices, res);

Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res) {
Expand Down
6 changes: 3 additions & 3 deletions codes/csharp/chapter_backtracking/subset_sum_i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public static void Backtrack(List<int> state, int target, int[] choices, int sta

/* 求解子集和 I */
public static List<List<int>> SubsetSumI(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
List<int> state = []; // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点
List<List<int>> res = new(); // 结果列表(子集列表)
List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, nums, start, res);
return res;
}

[Test]
public void Test() {
int[] nums = { 3, 4, 5 };
int[] nums = [3, 4, 5];
int target = 9;
List<List<int>> res = SubsetSumI(nums, target);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
Expand Down
6 changes: 3 additions & 3 deletions codes/csharp/chapter_backtracking/subset_sum_i_naive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public static void Backtrack(List<int> state, int target, int total, int[] choic

/* 求解子集和 I(包含重复子集) */
public static List<List<int>> SubsetSumINaive(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
List<int> state = []; // 状态(子集)
int total = 0; // 子集和
List<List<int>> res = new(); // 结果列表(子集列表)
List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, total, nums, res);
return res;
}

[Test]
public void Test() {
int[] nums = { 3, 4, 5 };
int[] nums = [3, 4, 5];
int target = 9;
List<List<int>> res = SubsetSumINaive(nums, target);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
Expand Down
6 changes: 3 additions & 3 deletions codes/csharp/chapter_backtracking/subset_sum_ii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ public static void Backtrack(List<int> state, int target, int[] choices, int sta

/* 求解子集和 II */
public static List<List<int>> SubsetSumII(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
List<int> state = []; // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点
List<List<int>> res = new(); // 结果列表(子集列表)
List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, nums, start, res);
return res;
}

[Test]
public void Test() {
int[] nums = { 4, 4, 5 };
int[] nums = [4, 4, 5];
int target = 9;
List<List<int>> res = SubsetSumII(nums, target);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
Expand Down
Loading