Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

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.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
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
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
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