Difference Between Console.Read() and Console.Readline()

Difference between Console.Read() and Console.ReadLine()?

Console.Read() reads only the next character from standard input,
and Console.ReadLine() reads the next line of characters from the standard input stream.

Standard input in case of Console Application is input from the user typed words in console UI of your application. Try to create it by Visual studio, and see by yourself.

Difference between Console.ReadLine and Console.In.ReadLine

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

What is the difference between read and readline in C#?

Do you mean TextReader.Read and TextReader.ReadLine?

One overload of TextReader.Read reads characters into a buffer (a char[]), and you can specify how many characters you want it to read (as a maximum). Another reads a single character, returning an int which will be -1 if you've reached the end of the reader.

TextReader.ReadLine reads a whole line as a string, which doesn't include the line terminator.

As far as I'm aware, endl is more commonly used in conjunction with cout in C++:

cout << "Here's a line" << endl;

In .NET you'd use

writer.WriteLine("Here's a line")

to accomplish the same thing (for an appropriate TextWriter; alternatively use Console.WriteLine for the console).

EDIT: Console.ReadLine reads a line of text, whereas Console.Read reads a single character (it's like the parameterless overload of TextWriter.Read).

Console.ReadLine() is basically the same as Console.In.ReadLine() and Console.Read() is basically the same as Console.In.Read().

EDIT: In answer to your comment to the other answer, you can't do:

int x = Console.ReadLine();

because the return type of Console.ReadLine() is a string, and there's no conversion from string to int. You can do

int x = Console.Read();

because Console.Read() returns an int. (Again, it's the Unicode code point or -1 for "end of data".)

EDIT: If you want to read an integer from the keyboard, i.e. the user types in "15" and you want to retrieve that as an integer, you should use something like:

string line = Console.ReadLine();
int value;
if (int.TryParse(line, out value))
{
Console.WriteLine("Successfully parsed value: {0}", value);
}
else
{
Console.WriteLine("Invalid number - try again!");
}

Console.ReadKey vs Console.ReadLine with a Timer

The Console.ReadKey() method locks the Console.InternalSyncObject whereas the Console.ReadLine() method does not. When the TimerCallBack() method tries to write to the Console the Thread waits because the Console.InternalSyncObject is still locked. Therefore GC.Collect() is never called. As soon as you hit a key the lock is released and GC.Collect() is called.

I changed your code to the following which doesn't lock the Console.InternalSyncObject and it only beeps once in Release and every 2 seconds in Debug.

private static void TimerCallback(Object o)
{
Console.Beep();
GC.Collect();
}

The reason the Console.WriteLine() waits is because it tries to acquire a lock on the Console.InternalSyncObject when creating the Console.Out TextWriter for the first time.

Changing your code to the following works as expected as we create the Console.Out TextWriter before starting the timer.

public static void Main()
{
Console.WriteLine("Loaded");
Timer t = new Timer(TimerCallback, null, 0, 2000);
Console.ReadKey();
}

private static void TimerCallback(Object o)
{
Console.WriteLine("In TimerCallback: " + DateTime.Now);
GC.Collect();
}

This is due to a change in .NET 4.5. More info here

Could someone explain why Converting to int and using read line instead of Read fixed my issue?

Console.Read() 

Returns you the ascii value of a single character being input.

For example, entering 'A' would return 65. See here for a list of ascii codes. Note that the ascii value for 1 is actually 49.

Convert.ToInt32(Console.ReadLine());

Reads the entire line and tries to convert it to an integer.

Console.Read() and Console.ReadLine() FormatException

I guess it is just because you pressing ENTER key after you type first number.
Lets analyze your code. Your code reads the first symbol you entered to a variable that Read() function does. But when you press enter key ReadLine() function returns empty string and it is not correct format to convert it to integer.

I suggest you to use ReadLine() function to read both variables. So input should be 7->[enter]->5->[enter]. Then you get a + b = 12 as result.

static void Main(string[] args)
{
Int32 a = 3;
Int32 b = 5;

a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());

Int32 a_plus_b = a + b;
Console.WriteLine("a + b =" + a_plus_b.ToString());
}

C#. Console.ReadLine() for two different obejcts

Console.ReadLine() does not return any "reader". It just returns String object which contains text grabbed from console input.

The line in method name means it will read everything available on the input until it will encounter line break.

If there is no line break, it will wait forever until user will press "enter" key.
If there are many text lines in the bufffer, you need to call ReadLine() many times.

You can assign player name diretly using:

 player1.Name = Console.ReadLine(); 


Related Topics



Leave a reply



Submit