为什么这个方法返回“System.String []”,而不是返回数组中的所有对象?

时间:2016-02-22 06:57:20

标签: c# arrays visual-studio

wordlist()方法,返回从结构中的字段复制的字符串数组。 但问题是此方法返回System.String[ ],而不是返回数组中的所有对象。我试图做foreach,但我认为不是问题。不幸的是,我无法弄明白。

public struct wordStruct
{
    public string Sword;
}   

class StringCounterClass : IOrederedWordStructure
{
    wordStruct[] array = new wordStruct[10];

    int totalInsertedWords = 0;

    public string[] wordList()
    { 
        string [] wordArray = new string[array.Length];

        for (int i = 0; i < wordArray.Length; i++)
        {
            wordArray[i] = array[i].Sword;
        }

       return wordArray;
    }

    public void insertWord(string[] words)
    {
        int insertwordLength = words.Length;
        totalInsertedWords = insertwordLength;
        string[] myWord = words;

        foreach (string word in myWord)
        {
            for (int i = 0; i < insertwordLength; i++)
            {
                array[i].Sword = myWord[i];
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

  1. 检查您的拼写错误

      } return wordArray;
    
    1. 您何时将值添加到代码中的数组对象中。如果没有正确完成,那么对象数组中的空数组大小为10。所以显然你会在你的方法中返回另一个空数组。