Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';我的方法不能使用大写字符串数组_C#_Loops - Fatal编程技术网

C# Can';我的方法不能使用大写字符串数组

C# Can';我的方法不能使用大写字符串数组,c#,loops,C#,Loops,后面列出了整个程序,但我唯一的问题是ToUppers()方法。我只希望这个方法迭代数组中的每个字符串,然后使所有内容都大写 private static string[] ToUppers(string[] stringToUpperArrays) { string stringer; foreach (string value in stringToUpperArrays) {

后面列出了整个程序,但我唯一的问题是
ToUppers()
方法。我只希望这个方法迭代数组中的每个字符串,然后使所有内容都大写

        private static string[] ToUppers(string[] stringToUpperArrays)
        {
            string stringer;

            foreach (string value in stringToUpperArrays)
            {
                stringer = value.ToUpper(); // <== this line highlighted
                Console.WriteLine(stringer);
            }
            return stringToUpperArrays;
        }

初始化阵列时,应擦除+1

stringArray = new string[(numInArray)];
用这种方法

 private static string[] CreateArray(int numInArray)

CreateArray
中,您将数组定义为
numInArray+1
的大小,但只能用
numInArray
字符串填充数组,即最后一个索引为空。当您尝试
value.ToUpper()时在空索引上,您会得到异常


CreateArray
中更改

stringArray = new string[(numInArray + 1)];

或改变

for (index = 0; index < numInArray; ++index)
{
    Console.Write("Enter string #" + (index + 1) + " ");
    stringArray[index] = Console.ReadLine();
}
for(索引=0;索引

for(索引=0;索引
这并不能回答问题
stringArray = new string[numInArray];
for (index = 0; index < numInArray; ++index)
{
    Console.Write("Enter string #" + (index + 1) + " ");
    stringArray[index] = Console.ReadLine();
}
for (index = 0; index < stringArray.Length; ++index)
{
    Console.Write("Enter string #" + (index + 1) + " ");
    stringArray[index] = Console.ReadLine();
}