public void ASimpleProgram()
The first program most coders learn to write is a “Hello World”. This simple program is very short and easy to understand. Below is an interactive example, using dotnetfiddle.net, a useful online C# compiler.
Here is the same program with comments for each line. Notice that a comment is created with two forward slashes. Anything written in the line after the two slashes will be ignored by the compiler.
Use one of the frames above to try changing the code that is printed. It should update as soon as you begin typing. If you have trouble running here, you can try to open a new tab to dotnetfiddle.net.
private void RunningLocally()
There are two Integrated Development Environments (IDEs) recommended for C#/.Net programming. While you can do a lot with dotnetfiddle.net, at some point you will find yourself wanting to create larger programs, and you will want to have an IDE.
Visual Studio is a full-featured IDE, which is used to create pretty much all Windows software on the market today, as well as a large amount of mobile and web projects. It is now available for Mac as well as Windows (although these are really still separate programs, so certain features may not be the same). While VS Community Edition is completely free, it is a very large program, and probably too much for starting out.
Visual Studio Code, despite it’s related name, is actually a much smaller program, with more focus on code writing/editing, and less features to support large projects. This actually makes it ideal for small projects and websites. VS Code is available for Windows, Mac, and even Linux.
Download and install Visual Studio Code. If you are running on Windows, we will make a settings change to use the
bash shell, so that the commands will be the same across platforms (skip to the next paragraph if you are on Mac/Linux). Go to
File->Preferences->Settings and add this line between the curly braces in
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
` or use the menu
View->Integrated Terminal to open the terminal window. Let’s create a new folder for working in. (For more info on terminal commands, see our Back in the Day post). Navigate to a place where you can create a folder, such as
cd /Users/username/Documents/ (add
c:/ to the beginning of the path for Windows). Make a folder with
mkdir Projects and enter that folder (
We will install one more piece of software, the .NET Core runtime, which is the cross-platform implementation of .NET. Once installed, return to VS Code, and type
dotnet new console -o HelloWorld into the terminal.
Now use the VS Code
File->Open Folder menu command and select your newly created HelloWorld folder. You should see something like this:
As you can see, this is essentially the same program that was written in DotNetFiddle, with a few variations. The
namespace creates an encapsulation for multiple files/classes to be able to reference themselves, but have protection from other linked files. The `
Main method now takes a string array of arguments, although currently they are not being used. We’ll show you what this is for later.
To run the program with .NET Core, you need to type the following command into the terminal:
dotnet run. The computer should think for a moment (compiling your code), and then return “Hello World!”. If you make changes to the
Program.cs file, such as updating the text to print, make sure you save the file before running again.