What is the Difference Between Console.Read() and Console.ReadLine() in C#?

C#.Net: Learn what are the differences between Console.Read and Console.ReadLine methods in C#.net, why and when they are used?
By IncludeHelp Last updated : April 06, 2023

Overview

In the last post, we have learnt, what are the differences between Console.Write() and Console.WriteLine() in C#.

In this post, we are going to learn what are the differences between Console.Read() and Console.ReadLine() methods in C#.Net? Since both are functions are used to take input from the standard input device, but they take different kind of values, here are the difference between them.

Console.Read()

It is used to read only single character from the standard output device (console).

Console.ReadLine()

It is used to read a line (or string) from the standard output device (console), a line may have many difference characters, it will read string until new line character (ENTER key) is not found.

Here, Console is the predefined library of System namespace from Framework Class Library and these both methods are associated with 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 :Hello India
String is: Hello India
Press any key to continue . . .




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.