C#应用程序异常void to string

Ele*_*ant -2 c#

我是C#的新手,我正在尝试在Console Appliciation中编写一个非常基本的scraper但是我收到错误:

Cannot implicitly convert type void to string
Run Code Online (Sandbox Code Playgroud)

我在下面的代码中尝试做的是将返回的输入设置为变量并将该变量返回到我的Main方法.这样主要方法可以访问和读取返回值,以便在需要时显示.

private static string setTargetModule()
{
    string targetUrl = Console.Write("Target: http://");
    return targetUrl;
}
Run Code Online (Sandbox Code Playgroud)

npi*_*nti 7

问题是,Console.Write("Target: http://")有没有任何回报,因为它是void.

要解决这个问题,您需要将文本输出到屏幕上,然后明确地将其读回.因此,您的代码将变为:

private static string setTargetModule()
{
    Console.Write("Target: http://");
    string targetUrl = Console.ReadLine();
    return targetUrl;
}
Run Code Online (Sandbox Code Playgroud)

或略短:

private static string setTargetModule()
{
    Console.Write("Target: http://");
    return Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)