Wednesday, December 5, 2018
Home > C# > Input Output in C#

C# Input Output

C# User Input

To take input from a user in C# we can use one of the following functions/ methods of Console class.

  • Read()
  • ReadLine()
  • ReadKey()

Read()

It is used to read a character from the user. And it returns Ascii value of that character.
Let’s have a look at the C# input output Example Program using Read() function.

using System;
namespace input
{
     class Program
     {
          static void Main(string[] args)
          {
               string str;
               Console.Write("Please Enter String : ");
               str = Console.Read().ToString();
               Console.Write("Ascci Value of Entered Character is : " + str + "\n");
          }
     }
}

Output
C# User Input - C# input output Example Program using Read()

ReadLine()

Another function that we use for C# user input is ReadLine. It reads a line from the console & returns the complete string from the console.
Here we have a C# input output Example Program using ReadLine() function.

using System;
namespace input
{
     class Program
     {
          static void Main(string[] args)
          {
               string str;
               Console.Write("Please Enter String : ");
               str = Console.ReadLine();
               Console.Write("You Entered String : " + str + "\n");
          }
     }
}

Output
C# User Input - C# input output Example Program using ReadLine() function

ReadKey()

It obtains the next key pressed by the user. It is usually used to hold the screen, and when a user presses a key, it will close the console screen.
Example Program using ReadKey() function.

using System;
namespace input
{
     class Program
     {
          static void Main(string[] args)
          {
               string str;
               Console.Write("Please Enter String : ");
               str = Console.ReadKey().ToString();
               Console.Write("You Entered String : " + str + "\n");
          }
     }
}

Output

C# User Input - C# input output Example Program using ReadKey() function

This below example program takes integer & double values from the console as input using ReadLine function.

using System;
namespace input
{
     class Program
     {
          static void Main(string[] args)
          {
               string str;
               int i;
               double d;

               Console.Write("Please Enter String : ");
               str = Console.ReadLine();
               // converting to integer
               i = Convert.ToInt32(str);
               Console.Write("You Entered this value : " + i + "\n");

               Console.Write("Please Enter String : ");
               str = Console.ReadLine();
               // converting to double 
               d = Convert.ToDouble(str);
               Console.Write("You Entered this value : " + d + "\n");
          }
     }
}

Output

C# input output example

Output in C#

In C# we can write to console using one of the following two functions of Console class.

  1. Write ()
  2. WriteLine ()

Both the above-written functions can be used to show output to a user. But there is a slight difference in between them, i.e., Write () only shows the string to the user, on the other hand, WriteLine () prints the string to console as well as it moves the cursor to the new line on the console.

Below given a simple example program explains their working as well as their difference.

using System;
namespace Output
{
     class Program
     {
          static void Main(string[] args)
          {
               Console.Write("This is shown uing Write() function!!!!!");
               Console.Write("\n");
               Console.WriteLine("This is shown uing WriteLine() function!!!!!");

               Console.Write("\n\n");
               Console.Write("Write Prints on ");
               Console.Write("same Line");
               Console.Write("\n\n");

               Console.WriteLine("WriteLine() Prints on");
               Console.WriteLine("New Line");
          }
     }
}

Output
Output in C#
Below example program will show how to write the values of variables to console using Write () and WriteLine ().
An example Program for output in c# using Write () and WriteLine () functions.

using System;
namespace Output
{
     class Program
     {
          static void Main(string[] args)
          {
               int i = 10;
               // using Write Function
               Console.Write("Value of i using write() function = " + i);
               // using WriteLine() Function
               Console.WriteLine("\nValue of i using writeLine() function = " + i);
               // this can also be done as 
               Console.WriteLine("Value of i using writeLine() function = {0}",i);
          }
     }
}

Output
C# output example

In the above program, we use the WriteLine function in two different ways to show output on the console. Similarly, We can also write variables of all other data types on the console using these functions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.