Your first program – Hello world

0 0
Read Time:5 Minute, 5 Second

Section 1. Introduction. 1.6 Your first program – Hello world

There are two ways to start Visual Studio Community 2019 – from Windows Start and from Visual Studio installer.

Click on Launch button to start Visual Studio Community 2019. If you need to install additional components click on Modify.

Launching Visual Studio 2019

Go to Start and click on Visual Studio 2019.

Windows 10. VS 2019 Start

Click on Create a new project.

Visual Studio 2019 start pop up

Choose Console Application from the list and click on the Next button.

Visual Studio 2019 select a new project from the list
Enter a name for your project, select the location to save your project files and click on the Next button. I would recommend to keep solution and project in the same directory.
Visual Studio 2019 configure your new project

Select the target Framework and click on Create button.

Visual Studio will automatically generate some code for your project.

C sharp Hello World program

Run the program by pressing the F5 button on your keyboard (or click on “Debug” -> “Start Debugging“). This will compile and execute your code.

Run the Hello World program by pressing the F5 button on your keyboard (or click on "Debug" -> "Start Debugging")

The result will look something to this.

The result of your first program Hello World will look something to this

Congratulations! You have now written and executed your first C# program.

Your first program explained

Line 1: using System means that we can use classes from the System namespace.

Line 2: A blank line. C# ignores white space but multiple lines makes the code more readable.

Line 3: namespace is used to organize your code, and it is a container for classes and other namespaces.

Line 4: The curly braces {} marks the beginning and the end of a block of code.

Line 5: class is a container for data and methods, which brings functionality to your program. Every line of code that runs in C# must be inside a class. In our example, we named the class Program.

Line 7: Another thing that always appear in a C# program, is the Main method. Any code inside its curly brackets {} will be executed. You don’t have to understand the keywords before and after Main. You will get to know them bit by bit while reading this tutorial.

Line 9: Console is a class of the System namespace, which has a WriteLine() method that is used to output (print) text. In our program it will output “Hello World!“.

If you omit the using System line, you would have to write System.Console.WriteLine() to print (output) text.

Please note, that:

  • Every C# statement ends with a semicolon ;
  • C# is case-sensitive: “myClass” and “myclass” has different meaning.
  • Unlike Java, the name of the C# file does not have to match the class name, but they often do (for better organization).
  • When saving the file, save it using a proper name and add “.cs” to the end of the filename. To run the example above on your computer, make sure that C# is properly installed.
  • The most common method to output something in C# is WriteLine(), but you can also use Write(). The difference is that WriteLine() prints the output on a new line each time, while Write() prints on the same line (you should remember to add spaces when needed, for better readability):

3 ways to run a C# program

There are 3 ways to compile and execute a C# program:

  • You can use an online C# compiler. You can use various online IDE which can be used to run C# programs without installing (Jdoodle, .Net Fiddle, Replit, Codingrooms etc.)
  • You can use Visual Studio IDE.
  • You can use Command-Line. It applies to .NET Framework projects only. You can also use command-line options to run a C# program. Below steps demonstrate how to run a C# program on Command line in Windows Operating System: You need open a text editor like Notepad or Notepad++, write the code in the text editor and save the file with .cs extension, open the cmd (Command Prompt) and run the command csc to check for the compiler version.The csc.exe executable file is usually located here – C:\Windows\Microsoft.NET\Framework\<Version>
Creating Hello World program in Notepad++
You can use Command-Line
Type csc to check compiler version on Windows 10

To compile the code type csc filename.cs on cmd. If your program has no error then it will create a filename.exe file in the same directory where you have saved your program. Suppose you saved the above program as helloworld.cs. So you will write csc helloworld.cs on cmd. This will create a hellowolrd.exe.

compiling the helloworld.cs program

You have two ways to execute the hellowolrd.exe. First, you have to simply type the filename i.e helloworld on the cmd and it will give the output. Second, you can go to the directory where you saved your program and there you find helloworld.exe. You have to simply double-click that file and it will give the output.

Type helloworld in cmd to start the program
Click on helloworld.exe file to start the program

How to modify PATH in MS Windows 10 (add path to csc.exe)

Click on Start and then click on Settings.

How to modify PATH (add path to csc.exe

Click on Settings.

How to modify PATH in MS Windows 10 (add path to csc.exe)

Click on About and then click on Advanced system settings.

How to modify PATH in MS Windows 10 (add path to csc.exe)

Click on Environment Variables.. button.

How to modify PATH in MS Windows 10 (add path to csc.exe)

Select Path in the User variables for … field and click on Edit… button. Then click on Browse.. button and look for C:\Windows\Microsoft.NET\Framework\<Version> or C:\Windows\Microsoft.NET\Framework64\<Version> . Click on OK button.

How to modify PATH in MS Windows 10 (add path to csc.exe)

Click on OK button. Congrats. You did it, the way to csc.exe added to the PATH. You can close all windows now.

Wrapping out

The Hello World! program is the most basic and first program when you dive into a new programming language. This program simply prints the Hello World! on the output screen.

Thank you for reading.

Check for more tutorials at acoptex.lt.

Check for Arduino and Raspberry Pi projects on our website acoptex.com.

Section 1. Introduction. 1.1 C# programming language

Section 1. Introduction. 1.2 Introduction to .NET Framework

Section 1. Introduction. 1.3 C# versions history

Section 1. Introduction. 1.4 C# vs Java

Section 1. Introduction. 1.5 C# get started

Section 1. Introduction. 1.6 Your first program – Hello world

Section 1. Introduction. 1.7 C# identifiers and keywords

Section 2. Fundamentals. 2.1 C# Comments

Section 2. Fundamentals. 2.2 C# Data types

Section 2. Fundamentals. 2.3 C# Constants and Literals

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *