Difference between Console.ReadLine and Console.In.ReadLine
Asked Answered
L

2

5

I came across these two APIs to read input from user in a simple console application in C#:

  • System.Console.ReadLine()
  • System.Console.In.ReadLine()

Here is a small code snippet where I'm trying to leverage these two:

namespace StackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            var input1 = System.Console.ReadLine();
            var input2 = System.Console.In.ReadLine();
         }
    }
}

When I run this program, the two lines of code are doing exactly the same thing i.e. whatever string I type on console is returned till the time I hit the return key. A quick googling fetched me only one link which differentiates between Console.Read and Console.ReadLine APIs. Can anyone help me in understanding the significance of these two separate APIs doing the same thing i.e. to receive user input?

Loudish answered 29/8, 2017 at 1:42 Comment(0)
A
7
System.Console.ReadLine()

Is an Alias for

System.Console.In.ReadLine()

So they are exactly the same.

Here is the code for ReadLine in Microsoft reference source.

[HostProtection(UI=true)]
[MethodImplAttribute(MethodImplOptions.NoInlining)]
public static String ReadLine()
{
    return In.ReadLine();
}

As you can see Console.ReadLine() just calls Console.In.ReadLine().

http://referencesource.microsoft.com/#mscorlib/system/console.cs,bc8fd98a32c60ffd

Apennines answered 29/8, 2017 at 1:58 Comment(0)
M
2

I would really just comment, but I do not have permissions.

These both links are explaining it very good. Look at examples to see a kind of production.

Gets the standard output stream.

Gets the standard input stream.

Here an another link to the Console.WriteLine Method It describes well, that it uses the standard output stream, so it means Console.Out.

Writes the specified data, followed by the current line terminator, to the standard output stream.

Moldy answered 29/8, 2017 at 2:5 Comment(1)
Thanks. That was helpful. You've the comment rights from now. Good luck with your journey on stackoverflow.Loudish

© 2022 - 2024 — McMap. All rights reserved.