12

Just ran across the problem described below. If "Console.TreatControlCAsInput = true;", you have to press [enter] twice on ReadLine().

I've written some demo code below. I am correct in surmising that this code demonstrate a bug in the .NET 4 framework?

        Console.Write("Test 1: Console.TreatControlCAsInput = false\nType \"hello\": ");
        {
            string readline = Console.ReadLine(); // type "hello" [enter].
            Console.WriteLine("You typed: {0}", readline);
            // Prints "hello".
        }

        Console.Write("Test 2: Console.TreatControlCAsInput = true\nType \"hello\": ");
        Console.TreatControlCAsInput = true;
        {
            string readline = Console.ReadLine(); // type "hello" [enter].
            Console.WriteLine("You typed: {0}", readline);
            // Should print "hello" - but instead, you have to press [enter] 
            // *twice* to complete the ReadLine() command, and it adds a "\r" 
            // rather than a "\n" to the output (so it overwrites the original line)
        }

        // This bug is a fatal error, because it makes all ReadLine() commands unusable.

        Console.Write("[any key to exit]");
        Console.ReadKey();
NotMe
  • 87,343
  • 27
  • 171
  • 245
Contango
  • 76,540
  • 58
  • 260
  • 305
  • What platform are you running this on? – Oded Nov 17 '11 at 14:30
  • 1
    I could reproduce this on Windows 7. Enter seems be doing the function of the **HOME** button – parapura rajkumar Nov 17 '11 at 14:35
  • 1
    I see it reported in 2006 where is is marked as closed, by design. http://connect.microsoft.com/VisualStudio/feedback/details/226101/console-treatcontrolcasinput-true-breaks-readline – automatic Nov 17 '11 at 14:42
  • 1
    @parapura rajkumar Actually, it seems to be printing \r (carriage return) instead of \n (line feed) when [enter] is pressed, and also requiring two [enter]s to be pressed to complete the ReadLine(). – Contango Nov 17 '11 at 16:20

2 Answers2

14

It is a known issue with the Windows Console subsystem and has been reported on Microsoft Connect back in 2006.

Posted by Microsoft on 22/05/2007 at 12:37

Hello ARos, Thank you for reporting this issue in System.Console. The behavior exists with the Windows Console subsystem, as demonstrated with the attached Win32 C application. I have reported the issue to the Windows Console subsystem owner.

Thanks, Josh

Johannes Kommer
  • 6,401
  • 1
  • 39
  • 45
  • 2
    As a side note: that connect item was closed as "by design". So Sounds like they decided it was a feature. – NotMe Nov 17 '11 at 14:39
  • @Chris Lively Amazing "feature" - it breaks ReadLine() so its unusable. I wonder how many developers have collided with this particular problem. – Contango Nov 17 '11 at 14:43
3

Not a bug on the Framework but it looks like a bug in the Windows console subsystem.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228