• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Libraries » Console Input-Output in C#
Next →
← Prev

Console Input-Output in C#

By Dinesh Thakur

We’ll be covering the following topics in this tutorial:

  • Console Input
  • Console Output

Console Input

The Console class allows using the Write () and the WriteLine () functions to display things on the screen. While the Console.Write () method is used to display something on the screen, the Console class provides the Read () method to get a value from the user. To use it, the name of a variable can be assigned to it. The syntax used is:

variableName = Console. Read();

This simply means that, when the user types something and presses Enter, what the user had typed would be given (the word is assigned) to the variable specified on the left side of the assignment operator.

Read() doesn’t always have to assign its value to a variable. For example, it can be used on its own line, which simply means that the user is expected to type something but the value typed by the user would not be used for any significant purpose. For example some versions of C# (even including Microsoft’s C# and Borland C#Builder) would display the DOS window briefly and disappear. You can use the Read() function to wait for the user to press any key in order to close the DOS window.

Besides Read(), the Console class also provides the ReadLine() method. Like the WriteLine() member function, after performing its assignment, the ReadLine() method sends the caret to the next line. Otherwise, it plays the same role as the Read() function.

 

string FirstName;

Console.write(“Enter First Name: “);

FirstName = console.ReadLine();

 

In C#, everything the user types is a string and the compiler would hardly analyze it without your explicit asking it to do so. Therefore, if you want to get a number from the user, first request a string. after getting the string, you must convert it to a number. To perform this conversion, each data type of the .NET Framework provides a mechanism called Parse. To use Parse(), type the data type, followed by a period, followed by Parse, and followed by parentheses. In the parentheses of Parse, type the string that you requested from the user.

 

Here is an example:

 

using system;

namespace GeorgetownCleaningservices

{

class orderprocessing

{

static void Main()

{

int Number;

string strNumber;

strNumber = console.ReadLine();

Number = int.parse(strNumber);

}

}

}

Console Output

Instead of using two Write() or a combination of Write() and WriteLine() to display data, you can convert a value to a string and display it directly. To do this, you can provide two strings to the Write() or WriteLine() and separate them with a comma:

1. The first part of the string provided to Write() or WriteLine() is the complete string that would display to the user. This first string itself can be made of different sections:

(a) One section is a string in any way you want it to display

(b) Another section is a number included between an opening curly bracket “{“and a closing curly bracket “}”.This combination of”{” and “}”is referred to as a, placeholder.

You can put the placeholder anywhere inside of the string. The first placeholder must have number O. The second must have number 1, etc. With this technique, you can create the string anyway you like and use the placeholders anywhere inside of the string

2. The second part of the string provided to Write() or WriteLine() is the value that you want to display. It can be one value if you used only one placeholder with 0 in the first string. It’ you used different placeholders, you can then provide a different value for each one of them in this second part, separating the values with a comma

Here are examples:

 

Demonstration of Writeline method

 

using System;

 

// An Exercise class

class Exercise

{

static void Main()

{

String FullName = “Anselme Bogos”;

int Age = 15;

double Hsalary = 22.74;

Console. writeline (“Full Name: {0}”, Full Name) ;

console.writeline(“Age: {0}”, Age);

console.writeline(“Distance: {0}”, Hsalary);

console.writeline(“Name: {0}\n Age: {1}\n Distance:{2}”,

FullName, Age, Hsalary);

Console.writeline();

}

}

 

OUTPUT:

 

Full Name: Anselme Bogos

Age: 15

Distance: 22.74

Full Name: Anselme Bogos

Age: 15

Distance: 22.74

 

As mentioned already, the numeric value typed in the curly brackets of the first part is an ordered number. If you want to display more than one value, provide each incremental value in its curly brackets. The syntax used is:

 

write(“To Display {0} {l} {2} {n}, First, second, Third, nth);

 

You can use the sections between a closing curly bracket and an opening curly bracket to create a meaningful sentence.

The System names pace provides a specific letter that you can use in the Write() or WriteLine()’s placeholder for each category of data to display. To format a value, in the placeholder of the variable or value, after the number, type a colon and one of the appropriate letter from the following table. If you are using ToStringO, then, in the parentheses of

ToString(), you can include a specific letter or combination inside of double-quotes. The letters and their meanings are:

                              Char used For

Use of particular character in writeline method

 

using System;

 

// An Exercise class

class Exercise

{

static void Main()

{

double Distance = 248.38782;

int Age = 15;

int Newcolor = 3478;

double Hsal = 22.74, Hrswork = 35.5018473;

double weeklysal = Hsal * Hrswork;

Console.writeLine(“Distance: {OJ”, Distance.ToString(“E”));

console.writeLine(“Age: {O}”, Age.ToStringO);

consol e.writeLine(“color: {O}”, Newcolor .Tostring(“x”));

Console.writeLine(“weekly salary: {OJ for .{1} hours”,

weeksal.ToString(“c”), Hrswork.ToString(“F”));

console.writeLine();

}

}

 

OUTPUT:

 

Distance: 2.483878E+002

Age: 15

color: D96

weekly salary: $807.31 for 35.50 hours

 

To specify the amount of space used to display a string, you can use its placeholder in Write() or WriteLine(). To do this, in the placeholder, type the 0 or the incrementing number of the placer and its formatting character if necessary and if any. Then, type a comma followed by the number of characters equivalent to the desired width. Here are examples:

 

Demonstration of Alinement in Writeline method

 

using System;

// An Exercise class

class Exercise

{

static void Main()

{

String Full Name = “Anselme 80gos”;

int Age = 15;

double Marks = 22.74;

 

console.writeline(“Full Name: {O,20}”, Full Name);

Console.writeline(“Age: {O,14}”, Age.ToString());

console.writeline(“Marks:{O:C,8}”, Marks.ToString());

Console. writeline(“——————————“) ;

Console.writeline();

}

}

 

OUTPUT:

 

Full Name:         Anselme bogos

Age: 15

Marks: $22.74

 

The sign you provide for the width is very important. If it is positive, the line of text is aligned to the right. This should be your preferred alignment for numeric values. If the number is negative, then the text is aligned to the left.

You’ll also like:

  1. How to Writing on Console Output
  2. Input/Output Operator in C++
  3. Input Output Statements in c++
  4. Input and Output Devices of Computer
  5. What is BIOS (basic input/output system)?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.