Reading string from user input in C#

Reading string from user input in C#

C# understanding string: Here, we will figure out how to take input (read a string) from the client in C#.Net?

To peruse a string from the client in C#, we use ReadLine() strategy for Console class.

Syntax:

    public static string Console.ReadLine();

open static string Console.ReadLine();

Here, we don’t pass any parameter and this strategy restores a string (a next line of the characters) from the info stream or returns invalid if there is no line of characters in the information stream.

The technique ReadLine() may return following special cases,

  • IOException: If there is an information/output exemption is happened.
  • OutOfMemoryException: If there is no adequate memory to store the client input.
  • ArgumentOutOfRangeException: If the string length if the more than the permitted number of characters (MaxValue).

C# example to peruse string from the client input:

using System;
    class JustTechReview {
    static void Main() {
        // declaring string variables
        String first_name = "";
        String last_name = "";
        
        // input the strings
        Console.Write("Enter first name: ");
        first_name = Console.ReadLine();
        Console.Write("Enter last name : ");
        last_name  = Console.ReadLine();
        
        // printing the values
        Console.WriteLine("Your name is: " + first_name + " " + last_name);
    }
}

Output:

Enter first name: Kishan
Enter last name : Durgesh
Your name is: Aakash

Recent Posts

error: Alert: Content is protected!!