Difference between Read and ReadLine

Updated on April 7, 2018 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on April 7, 2018

hi I am just starting out with c#. i am curious and confused about the difference of Read and Read Line in c# and what differ in their use? I hope you can help me guys.

  • Liked by
Reply
0 on April 7, 2018

Read gets the next character and converts it into ASCII code and return an int

//Entering a(lowercase)  will print 97 as per the ASCII table
int i = Console.Read();
Console.WriteLine(i);
Console.ReadKey();
//Entering a here will return a since the int is converted to a char again
int i = Console.Read();
char ch = Convert.ToChar(i);
Console.WriteLine(ch);
Console.ReadKey();

ReadLine just gets everything before enter key is pressed as a string

Note: Read() is almost never used, you almost certainly should use ReadLine and convert to int if you need to.

  • Liked by
Reply
Cancel