C#.Net: Learn what are the contrasts among Console.Read and Console.ReadLine techniques in C#.net, why and when they are utilized?
In the last post, we have realized, what are the diffrence among Console.Write() and Console.WriteLine() in C#.
In this post, we will realize what are the contrasts among Console.Read() and Console.ReadLine() strategies in C#.Net? Since both are capacities are utilized to take contribution from the standard info gadget, yet they take distinctive sort of qualities, here is the contrast between them.
Console.Read()
It is utilized to peruse just a single character from the standard output gadget (support).
Console.ReadLine()
It is utilized to peruse a line (or string) from the standard output gadget (support), a line may have numerous distinction characters, it will peruse string until newline character (ENTER key) isn’t found.
Here, Console is the predefined library of System namespace from Framework Class Library and these the two techniques are related to this class.
Example of Console.Read() in C#.Net:
using System;
namespace ReadLineEx
{
class Program
{
static void Main(string[] args)
{
char ch;
Console.Write("Enter single character :");
ch = Convert.ToChar(Console.Read());
Console.WriteLine("Character is: "+ch);
}
}
}
Output:
Enter single character :a
Character is: a
Press any key to continue . . .
Example of Console.ReadLine() in C#.Net:
using System;
namespace ReadLineEx
{
class Program
{
static void Main(string[] args)
{
string str;
Console.Write("Enter string of characters :");
str = Console.ReadLine();
Console.WriteLine("String is: "+str);
}
}
}
Output:
Enter string of characters :Welcome India
String is: Welcome India
Press any key to continue . . .