C# - 错误 "System.ArgumentOutOfRangeException: Length cannot be less than zero." 但作为参数放入 WriteLine 时没有错误?
C# - Error "System.ArgumentOutOfRangeException: Length cannot be less than zero." but no error when placed into WriteLine as parameter?
背景:
您好,我目前正在学习 C# 并在 HackerRank 进行一些实践。
所以我遇到了一个楼梯问题,我应该编写代码以接收整数输入,然后使用空格和哈希输出楼梯 'diagram'。
面临的挑战:
下面的代码给我一个运行时错误,上面写着“System.ArgumentOutOfRangeException:长度不能小于零。”
// Complete the staircase function below.
static void staircase(int n) {
// Find number of spaces needed
string space = "";
for (int i = 1; i < n; i++) {
space += " ";
}
string hash = "#";
for (int j = 0; j < n; j++) {
space = space.Substring(0, space.Length - j);
Console.WriteLine(space + hash);
hash += "#";
}
}
但是,当我将第二个 for
循环中的代码从
更改为
space = space.Substring(0, space.Length - j);
Console.WriteLine(space + hash);
到Console.WriteLine(space.Substring(0, space.Length - j) + hash);
然后它运行成功,我看不出有什么不同,我很困惑为什么它会起作用?
这两个不一样。
这一行:
Console.WriteLine(space.Substring(0, space.Length - j) + hash);
类似于:
string temporarySpace = space.Substring(0, space.Length - j);
Console.WriteLine(temporarySpace + hash);
在那里,您没有修改变量space
,而是创建了一个临时变量
我更改了您的代码,以便您可以看到此行为:
https://dotnetfiddle.net/ZhXIWa
for (int j = 0; j < n; j++) {
var tempSpace = space.Substring(0, space.Length - j);
Console.Write($"space: {space.Length} | ");
Console.Write($"tempSpace: {tempSpace.Length} | ");
Console.Write(space + hash);
Console.Write(Environment.NewLine);
hash += "#";
}
// Output
space: 9 | tempSpace: 9 | #
space: 9 | tempSpace: 8 | ##
space: 9 | tempSpace: 7 | ###
space: 9 | tempSpace: 6 | ####
space: 9 | tempSpace: 5 | #####
space: 9 | tempSpace: 4 | ######
space: 9 | tempSpace: 3 | #######
space: 9 | tempSpace: 2 | ########
space: 9 | tempSpace: 1 | #########
space: 9 | tempSpace: 0 | ##########
在那里你会看到两个变量(space 和 temporarySpace)有多少个字符。您可以将代码修改为不起作用的代码,您会看到不同之处:)
背景:
您好,我目前正在学习 C# 并在 HackerRank 进行一些实践。
所以我遇到了一个楼梯问题,我应该编写代码以接收整数输入,然后使用空格和哈希输出楼梯 'diagram'。
面临的挑战:
下面的代码给我一个运行时错误,上面写着“System.ArgumentOutOfRangeException:长度不能小于零。”
// Complete the staircase function below.
static void staircase(int n) {
// Find number of spaces needed
string space = "";
for (int i = 1; i < n; i++) {
space += " ";
}
string hash = "#";
for (int j = 0; j < n; j++) {
space = space.Substring(0, space.Length - j);
Console.WriteLine(space + hash);
hash += "#";
}
}
但是,当我将第二个 for
循环中的代码从
space = space.Substring(0, space.Length - j);
Console.WriteLine(space + hash);
到Console.WriteLine(space.Substring(0, space.Length - j) + hash);
然后它运行成功,我看不出有什么不同,我很困惑为什么它会起作用?
这两个不一样。
这一行:
Console.WriteLine(space.Substring(0, space.Length - j) + hash);
类似于:
string temporarySpace = space.Substring(0, space.Length - j);
Console.WriteLine(temporarySpace + hash);
在那里,您没有修改变量space
,而是创建了一个临时变量
我更改了您的代码,以便您可以看到此行为: https://dotnetfiddle.net/ZhXIWa
for (int j = 0; j < n; j++) {
var tempSpace = space.Substring(0, space.Length - j);
Console.Write($"space: {space.Length} | ");
Console.Write($"tempSpace: {tempSpace.Length} | ");
Console.Write(space + hash);
Console.Write(Environment.NewLine);
hash += "#";
}
// Output
space: 9 | tempSpace: 9 | #
space: 9 | tempSpace: 8 | ##
space: 9 | tempSpace: 7 | ###
space: 9 | tempSpace: 6 | ####
space: 9 | tempSpace: 5 | #####
space: 9 | tempSpace: 4 | ######
space: 9 | tempSpace: 3 | #######
space: 9 | tempSpace: 2 | ########
space: 9 | tempSpace: 1 | #########
space: 9 | tempSpace: 0 | ##########
在那里你会看到两个变量(space 和 temporarySpace)有多少个字符。您可以将代码修改为不起作用的代码,您会看到不同之处:)