在C#中是否可以将数组返回给调用程序?

在C#中是否可以将数组返回给调用程序?,c#,C#,在C#中是否可以将数组返回给调用程序?如果这是不可能的,请说这不是所有的可能。另一种方法是创建一个长字符串并使用string.split()。但这看起来不太好。 examplationofreturnsfiled(“ABCDE1234E”)//调用程序 public yearsfiled[] ExamnationOfReturnsFiled(string panreceived) //function. { int k = 0; //to increment t

在C#中是否可以将数组返回给调用程序?如果这是不可能的,请说这不是所有的可能。另一种方法是创建一个长字符串并使用string.split()。但这看起来不太好。 examplationofreturnsfiled(“ABCDE1234E”)//调用程序

public  yearsfiled[] ExamnationOfReturnsFiled(string panreceived) //function.
{            
    int k = 0; //to increment the array element.
    string item = panreceived; //string value received call program.

    string[] yearsfiled = new string[20];//Declaring a string array.
    Regex year = new Regex(@"[0-9]{4}-[0-9]{2}");//to capture 2012-13 like entries.
    using (StreamReader Reader1 = new StreamReader(@"C: \Users\Unnikrishnan C\Documents\Combined_Blue_Book.txt"))
    {
        Regex tofindpan = new Regex(item);//Regular Expression to catch the string from the text file being read.
        bool tosearch = false;
        Regex blank = new Regex(@"^\s*$"); //to detect a blank line.
        while ((str.line1 = Reader1.ReadLine()) != null)
        {
            Match Tofindpan = tofindpan.Match(@"[A-Z]{5}[0-9]{4}[A-Z]{1}");
            Match Blank = blank.Match(line1);
            if (Blank.Success)
            {
                tosearch = false;
            }
            if (Tofindpan.Success)
            {
               tosearch = true; //when true the 
            }
            if (tosearch == true)
            {
               Match Year = year.Match(str.line1);
               if (Year.Success)
               {
                   yearsfiled[k] = Year.Value;
                   k++;
               }
            }
        }
        return yearsfiled;
    }

}

您应该返回一个
字符串[]
。您的返回类型
yearsfiled[]
是一个变量名,而不是类型名

公共字符串[]examplationofreturnsfiled(string panreceived)
//函数


您正在从调用程序返回类型而不是变量名更改上述方法签名。测试成功

string[] yearsfiled = new string[20];
yearsfiled = ExamnationOfReturnsFiled(item1);
//函数名修改如下

public static string[] ExamnationOfReturnsFiled(string panreceived)
{
  Everything else as in the original post.
}

//它经过测试。并获得成功。非常感谢@Midhun Mundayadan和@Eavidan

如果您将返回年份移到while循环之外,您可能有更好的机会从这段代码中获得一些东西。返回应该移到外部。那是我的错误。完成。当然,可以返回一个数组。但是,您应该彻底修改代码的逻辑。例如,panreceived的作用是什么,或者检查非空行。我的问题现在解决了。不过,我想回答Cetin Basoz提出的问题。程序读取每一行,以找出正则表达式格式[A-Z]{5}[0-9]{4}[A-Z]{1}中的某些内容。但我需要捕获的不是全部,而是与正则表达式匹配的某些预定条目。然后捕获过程开始,布尔值变为“真”。它必须停止一旦有一个空白行,因为无论是在正则表达式和空白行之间的谎言,我只需要进一步处理。谢谢你@Cetin Basoz。我明白了,但你的代码不是那样工作的。找到匹配项后,它不会停在空行上。