Seven Things about C#

A beginners tutorial, where each article highlights 7 important things about a specific feature of the C# programming language.

Follow publication

7 Things about C#: Console I/O

Joe Mayo
Seven Things about C#
8 min readJun 27, 2023
A dark, but cute and furry, cat looking animal.

The console is the command line, where you type commands to run code and get a response. In general terms, input and output (I/O) describe the interactions of information being given to the program (input) and information that the program emits.

These interactions are often communication with a user, but can also be communication with other programs. We won’t get that elaborate here, but will describe some interesting I/O capabilities that you can use in a C# application to communicate with people.

1 — There are different ways to write

The Console class has two writing methods: Write and WriteLine. The difference is that WriteLine adds a new line after printing and Write doesn’t. Here’s an example:

Console.WriteLine("Console I/O Demo\n");

Console.WriteLine("\t 1. Door #1");
Console.WriteLine("\t 2. Door #2");
Console.WriteLine("\t Q. Quit");

Console.Write("\nYour Choice (1, 2, or Q): ");

You’ve seen Console.WriteLine in the previous post on Running Apps. However, there are a few differences here, specifically in the strings with escape, \, characters, which are special instructions to the terminal. The first line contains a newline escape, \n, which adds a line in the terminal where it appears. This example has the effect of adding two newlines because one is added by the call to WriteLine by design.

The 2nd through 4th lines have a tab, \t, prefix. This helps to indent each menu options, adding visual cues to the output.

The last line is different because it uses Console.Write, which doesn’t add a newline to the end of the string. Notice that this is a prompt, where the user would type a letter from the menu and, in this design, we want what they type to appear on the same line.

Another interesting feature is the use of the newline escape, \n, prefix in the string for Console.Write. We could have added the newline to the last menu item. However, I thought it was interesting to show another example of how to use escape characters to help format string output. We’ll see more formatting examples both in this post and throughout the series, including when we discuss strings in more depth.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Seven Things about C#
Seven Things about C#

Published in Seven Things about C#

A beginners tutorial, where each article highlights 7 important things about a specific feature of the C# programming language.

Joe Mayo
Joe Mayo

Written by Joe Mayo

Author, Instructor, & Independent Consultant. Author of C# Cookbook: — http://bit.ly/CSharpCookbook — @OReillyMedia #ai #csharp #Web3

Responses (1)

Write a response