// 01. Introduction
The C# Console Class is the perfect place to start in your path as a C# developer. “Why“, you might ask? Well, you need a few things that the Console Class offers for future projects, such as displaying content to the user in the console window, as well as getting input from the user. It also gives you the chance to get somewhat familiar with reading the C# developer documentation that is provided by Microsoft.
Console Class Documentation can be found here:
MSDN Console Class Documentation
// 02. Properties
The C# Console Class has properties. A property is a variable that is attached to the object from the class, in this case, the Console object from the Console Class, that can be publicly accessed; meaning that you can either get the property’s value or set it without editing the Class definition directly. We will talk more about properties later when we discuss Object Oriented Programming (please don’t call it “Oop”, but “Oh”, “Oh”, “Pee”). If you were a computerized version of yourself … sitting in a computer program, then your name would be a property, as well as your age, height, weight, language(s) spoken, IQ, number of children, if you ever were – or are currently married, your social security number, where you live – virtually every piece of information about you would be a property.
Notable Properties of the Console Class
- Console.BackgroundColor
- Console.ForegroundColor
- Console.Title
See the video below for a walkthrough of the properties.
// 03. Methods
The C# Console Class also has methods. Methods, in contrast to properties, do things. Think of methods as the actions an object can take. In this case, our object is the Console Window itself, and it can DO a lot. Again, if you were in a computer program, then you would have methods too, such as waking up, brushing teeth, taking a shower, talking, using the necessities, and complaining about bad drivers texting while they are on the road. Virtually everything you do would be a method. So, let’s take a look at some methods from the Console Class.
Notable Methods of the Console Class
//------------------------------------------ Console.Beep(); // The Console Window makes a beep noise. Console.Clear(); // Clears the Console Window. Console.ReadKey(); // Use to pause the program until a any key is pressed. string userInput = Console.ReadLine(); // Use to get input from the user (will always be a string value) Console.Write("String to display...") // Writes content on a single line WITHOUT a carriage return. Console.WriteLine("String to display...") // Writes content on a single line WITH a carriage return - '\n'. //------------------------------------------
See the video below for a walkthrough of the methods.
// 04. Displaying to the Console Window
It is important to be able to give the user as much information as is necessary to accomplish the expected tasks purposed for your application. When working in the console window, the Console.Write() and Console.WriteLine() methods are used.
The Console.Write(“…”) method is best used when asking for an answer from the user, a process known as prompting the user for input.
The Console.WriteLine(“…”) method is best used when presenting a question to the user, displaying a menu, displaying header information to give the user orientation within the program, etcetera.
Note: Rather than typing what you want to say in the parenthesis using quotation marks “…” (known as hardcoding), you can also store your “…” (string literal) in a string variable, and then display it using the method.
//------------------------------------------ // Storing the string literal in a variable string greeting = "Hello!"; // Display to the user Console.WriteLine(greeting) //------------------------------------------
// 05. Capturing Input From the User
In all useful applications, capturing user input is vital. With user input, we will then be able to give the user limited control over how the application will run. On a smartphone, half of the battle is won by controlling the user’s choices through triggering actions with a button, but in the case of a console application, we don’t have the luxury of displaying buttons (also known as a User Interface Element). So, we must allow the user to make decisions from their keyboard.
For capturing user input, we use Console.ReadLine(). The one thing we CAN’T FORGET ABOUT is to place a string variable in front of this method call, because it will return whatever the user entered. If you don’t catch the returned value, it will be lost forever!
//----------------------------------------- // Declare a variable to capture user input string userInput; // Prompt for input Console.Write("What is your name: "); userInput = Console.ReadLine(); // Display what the user entered Console.WriteLine("Your name is " + userInput + "!"); //-----------------------------------------
See the video below for an example of one prompt and capture of user input.
Leave a Reply