Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Console.ReadLine and Console.In.ReadLine

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?

like image 454
RBT Avatar asked Aug 29 '17 01:08

RBT


People also ask

What is console in ReadLine?

The Console. ReadLine() method in C# is used to read the next line of characters from the standard input stream.

What is the difference between console WriteLine () and console ReadLine ()?

ReadLine method to read each line in the file, replaces every sequence of four spaces with a tab character, and uses the Console. WriteLine method to write the result to the output file.

What is difference between read () and ReadKey ()?

The Read() reads the next characters from the standard input stream. If a key is pressed on the console, then it would close.

What is difference between console write and console WriteLine?

The only difference between the Write() and WriteLine() is that Console. Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.


1 Answers

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

like image 178
Xela Avatar answered Sep 28 '22 09:09

Xela