If you are reading this guide, I am assuming that you have already downloaded UnityHub and Installed the desired unity version on your system, if not then goto to unity website and download the UnityHub application to begin the installation process and also you have some familiarity with C# basic syntax. Typically, UnityHub also recommends to download Visual Studio along with its editor when downloading any version of Unity. While installing your desired version, make sure that “Install Visual Studio” checkbox is also selected.
Now that you have Unity installed along with Visual Studio, you might be tempted to just open the Unity engine and get started with creating your first game. But hold your horses there! Before you learn to run, you need to learn how to walk. And that is exactly we are going to do in this guide. I am going to go through the basics of C# using Visual Studio only. I am using Visual Studio 2022, even if your version is different than mine, in most cases this should not cause any issue.
We are going to take project based approach where we will be making a basic calculator application which will take 2 numbers as the input and print the sum of two numbers as the output on screen.
Following should be the expected outcome from this guide :
Alright, so lets dive in! First, launch visual studio, and click on “Create a new project” tab as shown below.
Now, you will be presented with a screen where you have to select a project template. As shown in the image below, select C# as the language type from the drop down.
Now, I am assuming that you have done the standard unity installation in which only unity related component is installed with your visual studio IDE. In order to be able to make a C# project for windows, you need to install additional component that supports development for windows platform using C#. So, after selecting the C# language from the dropdown, if you don’t see any template available, you would notice there will be a button/link which will be asking you to install additional component, click on it.
Now, you will see bunch of different components that can be installed in your visual studio IDE to support different types of development, the one that you have to choose is “Universal Windows Platform Development” module, make sure it is checked and then click install.
Note: You may see a dropdown containing two options – Install while downloading and install after all downloaded, choose any of the option based on your internet speed and stability. If you are unsure, leave it blank.
Now that all required component is installed, its time to create our first C# project! Re-Launch the visual studio IDE and click on create new project, now make sure that under language dropdown C# is selected, you will different options to create a project using C#. Select “Console App” from the template list and click “Next”.
On the next screen, you will be presented with few options to configure your project. And if you have never worked with visual studio, the very first confusion you will have is, what is the difference between a project and a solution? Well fortunately it is not that hard, let me explain this.
Project – A project is basically a place where all your code files and related assets will be there which, all together, creates your application.
Solution – A solution is, simply said, a container in which there can be multiple projects.
Now, let’s try to understand this concept with a practical use-case. In our case, we just have to make a simple calculator where we have only one script file which is responsible for handling input and displaying output. But, if we have to create a large scalable complex application which have different components, then sometimes it becomes quite a requirement to split the application into different chunks of smaller independent working components which all together function as a single application. And these smaller chunks can be multiple projects which can have different functionalities, so a solution would be a safe place to keep all these independent projects together at a single place.
Alright, so as shown in the image below, enter a proper name for your project, in my case I have given a name LearnCoding, you can give any name as you like and make sure to check the box labelled “Place solution and project in the same directory”. Since we are just creating a small single project, we don’t really need a separate folder for solution and project, so checking this option will save some complexity of our project structure.
Once you click “Next” there will be an additional popup that will be asking you to choose a .NET version, leave everything as default and click “Create”. Now wait for some time until visual studio finishes creating the brand-new project for you.
Now, if everything is correct and project creation is successful, you should see an editor window with the code as below :
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
The visual studio has not only created a project for you, but also created a default C# script named “Program.cs” in which it has added the famous default “Hello World” message which will be printed on the console. Now if all this is not making any sense, don’t worry, for now just press on the green play like button with the name of your project, in my case it is LearnCoding.
As soon as you click on that button, visual studio will start building the project and soon you will see a command prompt interface will pop up which is known as the “console”. And exactly for what we are building this application. Our application is going to be a console app where the user will see and provide its input on that console.
So, let’s move forward, when pressing on that build button, you should see your console popped up something like this:
Now, let’s proceed further. Before writing our first line of code, let us understand the required logic first. We need to build a simple calculator app that would require the user to enter two numbers one by one and then it will display the sum of both numbers on the console. Now, overall code for this requirement would look something like this:
//Declare and initialize the variables
int num1 = 0;
int num2 = 0;
int sum = 0;
//Print the welcome message and ask the user to enter the first number
Console.WriteLine("Hello, welcome to my simple application");
Console.WriteLine("Please enter the first number");
//Get the first number from the user
num1 = int.Parse(Console.ReadLine());
//Ask the user to enter the second number
Console.WriteLine("Please enter the second number");
//Get the second number from the user
num2 = int.Parse(Console.ReadLine());
//Calculate the sum of both numbers and store it in the third variable
sum = num1 + num2;
//Print the result back to the user
Console.WriteLine("The sum of both numbers is : " + sum.ToString());
Now let us understand what we have written here, first and foremost we have declared 3 variables which are num1, num2 and sum. As you might have guessed already, num1 and num2 are the variables which are going to be responsible for storing the 2 values that user is going to provide and the third variable sum, is going to store the total sum of both values.
Then, we are printing few lines of message to greet the user and prompt the user to enter the first number, then we are getting the first number, then prompting the user again to input second number and we are performing the add operation to sum up both the numbers and storing it into the third designated variable and finally printing of the final result to the user. Feel free to copy the code above or write it yourself and press the build button to see the output.
Congratulations! You have just learnt how to create a console application for windows using C#.