Learn how to build a simple C# console application. I go through the code line by line for an easy way to learn some of the great features C# has to offer!
Prerequisites
You’ll learn how to setup a simple C# application so you will need to have Visual Studio downloaded.
Creating the Application
Open Visual Studio, click File -> New -> Project -> Click the drop down menu on the left side of the window that pops up and click Visual C#. This should show all the different type of project templates you can choose from. For this tutorial, I will be creating a Console App. Go ahead and Click “Console App (.NET Framework)” and give your application a name. I will Name it “ConsoleAppTutorial”. Click OK to Create your new project.
The code below is what will show up on your screen. I will go through the code line by line:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleAppTutorial { class Program { static void Main(string[] args) { } } }
Lines 1 through 5 are your Using Directives. These are the namespaces that hold all the functions you can use in your application. Think of these lines as a library, and each Using Directive is a book you can reference from and take sources from. This way, you don’t need to re-write code that is readily available since someone has done that for you. No need to reinvent the wheel 🙂
Line 7 has the name of your application. Going back to lines 1 through 5, all the code/functions you have written here, you can compile together, and reference in a whole different application just by putting it in your Using Directives. Cool, huh?
Lets look at 9 and line 11. Line 9 is the class that the function Main on line 11 will be defined in. A class is basically a collection of different objects. the function Main is defined as a “static void”. A static type of function means that you don’t need to initialize a “Program” object in order to use it.
Also in line 11, the Main function has a string[] args argument that is used to pass in values from the command line. For example, if I wanted to create an application that writes the same string that I pass to it, I could call the program from the command line and also write the string I want the program to write.
Lets change the code of the program to do this. In the static void Main function type the following:
static void Main(string[] args) { Console.Writeline(args[0]); }
Build your program by pressing F6, go to the folder where your project was created by right-clicking the project on the right hand side of Visual Studio, and selecting “Open File in Folder Explorer”. then go to bin -> Debug folders. In this folder, press and hold ctrl and right click, then choose “Open command window here”. This will open a command prompt window. In the window, type “ConsoleAppTutorial Hello!”. This will output the word Hello!
Hopefully this tutorial was helpful enough. If you have an comments or would like me to describe another topic, please don’t hesitate to write a comment below! If you need any more references to go off of, I would highly recommend to go to the microsoft docs webpage: https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/console-teleprompter