简体   繁体   中英

C# console application ReadLine() works in .NET 4.7.2 and not in .NET 5 (on dotnetfiddle)

As I had never written a console application, I thought it was a good idea to start now. If only because of the fact that I find Visual Studio rather sluggish and the idea to be able to test code quickly on one of the playgrounds such as dotnetfiddle seems attractive. The default compiler on dotnetfiddle.net is on .NET 4.7.2 but I noticed that that one choked on the more recent code I borrowed. So I got used to the fact that I have to switch the compiler to .NET 5 to be able to take advantage of for instance niceties such as the $ to format text.

TL;DR

However, I found that a simple ReadLine() such as found in the example on https://dotnetfiddle.net/tAJulh doesn't work if you change the compiler to .NET 5. Has something changed in the specification of ReadLine in .NET 5 or is this a limitation of dotnetfiddle? And is there a way around this?

using System;
                    
public class Program
{
    public static void Main()
    {   
        // Simply writes the following text to the standard output
        Console.WriteLine("Input some text!");
        
        // Reads text until you hit "enter"; and saves it to the "input"-variable
        var input = Console.ReadLine();
        
        // Writes the "input"-variable to the console.
        Console.WriteLine("Your input was: " + input);
        
        // The program will not exit until you hit "Enter".
        Console.ReadLine();
    }
}

You are absolutely right. Does look like an error on dotnetfiddle and you should report it here: https://dotnetfiddle.uservoice.com/forums/228764--net-fiddle-ideas

As far as i can tell, there is no way around it before they correct the bug.

As some other guys have stated i highly recommend that you get an IDE, this could be visual studio code or the plain visual studio

Personally i enjoy VS code for it's simplicity and it works great for C# and .net

I can confirm that Console.ReadLine isn't supported at the moment by dotnetfiddle and our's .NET Core implementation. The reason is that we use sandboxes since we need to be safe when we execute user's code. And for .NET 5 we are using docker while for regular .NET 4.7.2\Roslyn we are using AppDomains. In the docker case we are just compiling code and execute it as is. In the AppDomain case we are restricting it by injecting own code on top of user's code.

And since these ways are different, each of them might have some limitations over other. The problem with ReadLine is that when user runs own code, we need to partially execute it, then inject into Console.In stream and if something is requested from the stream, then we stop execution and ask user to provide input, and then after input is provided, we need to restart execution again and use that user's input in Console.In and repeat it over and over until code is fully executed. It's much easier to inject it for AppDomain so it works fine in .NET 4.7.2 but harder to do it in docker since we don't inject own code there so this feature is missing for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM