Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Input & Output in C#

C# Basic Input and Output

In this tutorial, we will learn how to take input from user and and display output in C# using various methods

C# Output

In order to output something in C#, we can use

System.Console.WriteLine() OR
System.Console.Write() 

Here, System is a namespace, Console is a class within namespace System and WriteLine and Write are methods of class Console.If we are using , “using System” then we can simply print like this Console.Write(“Something”);

The main difference between WriteLine() and Write() is that the Write() method only prints the string provided to it, while the WriteLine() method prints the string and moves to the start of next line as well.

Strings can be combined/concatenated using the + operator while printing.

Let’s look at a simple example that prints a string to output screen.

 

using System;

namespace Sample{
    class Test{
        public static void Main(string[] args){
            // Printing strings
             Console.WriteLine("Interviewbit is cool");
            // Printing integers
            Console.WriteLine(10);
            // Printing variables
            int a = 100;
            Console.WriteLine(a);            
            // Printing Concatenated String using + operator
            int val = 55;
            Console.WriteLine("Hello " + "Interviewbit");
            Console.WriteLine("Value = " + val);
        }
    }
}

When we run the program, the output will be

Interviewbit is cool
10
100
Hello Interviewbit
Value = 55

Input

In C#, the simplest method to get input from the user is by using the ReadLine() method of the Console class. However, Read() and ReadKey() are also available for getting input from the user. They are also included in Console class.

Read() - The Read() method reads the next character from the standard input stream. It returns the ascii value of the character.

ReadLine() - The ReadLine() method reads the next line of input from the standard input stream. It returns the same string.

ReadKey() - The ReadKey() method obtains the next key pressed by user. This method is usually used to hold the screen until user press a key.

Reading numeric values can be slightly tricky in C#. We’ll still use the same ReadLine() method we used for getting string values. But since the ReadLine() method receives the input as string, it needs to be converted into integer or floating point type.

One simple approach for converting our input is using the methods of Convert class.

Let’s look at a simple example that reads inputs and print it to output screen.

 

using System;

namespace UserInput{
    class MyClass{
        public static void Main(string[] args){
            string userInput;
            int intVal;
            double doubleVal;
Console.Write("Enter a string: ");
userInput = Console.ReadLine();
Console.WriteLine("You entered " + userInput ); Console.Write("Enter integer value: "); userInput = Console.ReadLine(); /* Converts to integer type */ intVal = Convert.ToInt32(userInput); Console.WriteLine("You entered " + intVal); Console.Write("Enter double value: "); userInput = Console.ReadLine(); /* Converts to double type */ doubleVal = Convert.ToDouble(userInput); Console.WriteLine("You entered " + doubleVal); } } }

When we run the program, the output will be

Enter a string: Interviewbit
You entered Interviewbit
Enter integer value: 101
You entered 101
Enter double value: 59.412
You entered 59.412

Task

Try taking a user input and printing space separated “Hello” and input (without quotes) in the editor below.

Input Format

Read a string input.

Output Format

Print Hello input to stdout.

Example Input

InterviewBit

Example Output

Hello InterviewBit
Start solving Input & Output in C# on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test