Creation of a few simple C# programs
The usual simplest of the simple C# Programs. Following the steps of how to create a New Project for a C# Console Application.
- Open Visual Studio
- File -> New Project
- C# Console Application
- Change the name to Hello World, the Solution name will automatically change to match it.
- Click OK to create an empty C# application
- In order to print “Hello World” to the console, we must use Console.WriteLine(“”);
Following the WriteLine with a ReadLine will allow us to actual view what was put on the screen by having the program wait for the user to enter something.
- Hitting Start or using the Shortcut F5, a Command Prompt will appear with the words Hello World! and will not exit until the user inputs something like hitting enter.
This next program will just be an example using variables to compute values and display them to the screen.
Creation of a new project for variables example:
In the following code two variables are created, x and y, which are declared as integers. After the variables are declared the x is defined as 3 and y is defined as x’s value plus 7.
Once the code is ran, you can see that the value of y is 10.
There is a special variable type called var that assigns the correct type of variable based upon what you define it as.
As you can see in the following code, the var keyword is used two different variables, one with an intenger and one with a string.
var x = 1; var y = "Hello World";
The code also displays how a variable that had it’s type declared can have the value defined as soon as it’s declared or later in the code.
int x; int y = 7; x = 3 + y;
When the code runs, the output shows the value of y and the two var keyword variables.