簡體   English   中英

錯誤返回關鍵字不得在對象表達式之后

[英]error return keyword must not be followed by an object expression

回報(匹配)讓我頭疼! 當我將其更改為Console.WriteLine(match)時,它給了我期望的收益,但是當我嘗試使用return(match)時,給了我一個錯誤。 我只是不知道在這里要更改什么,所以任何建議將不勝感激!

問候,詹姆斯

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ReadTextFile
{
    class Program
    {
        static void Main(string[] args)

        {

            List<string> fileLines = new List<string>();

            using (var reader = new StreamReader("test.txt"))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    fileLines.Add(line);
                    string pattern = @"(\w+)@(\w+).([a-z]+)";
                    Match match = Regex.Match((line), pattern);
                    if (match.Success)
                    {
                        return(match);                        
                    }


                }

            }

如果要返回其他內容,則Main返回一個void ,更改簽名。 請注意,通常Main的返回值稱為錯誤代碼,其中非零被視為錯誤。

如果創建另一個函數並將邏輯放入其中,則可以使用return。

另一方面,如果您要做的只是離開while您需要進行Match match; string line; 並使用break; 離開而不是return

您處於void方法( void Main )中。 void方法沒有返回值,因此只需return; 本身就是所允許的。

順便說一句, return不是C#中的函數,它是一條語句,因此不需要括號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM