Ozeki 10 SMS Gateway

C# tutorial: Hello world

This tutorial helps you get started with C#. It explains what you need to write your first C# program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and runing your code. If you have never written a C# program before this is the place to start. We have created this article, so you can get started with C#, and move on to our sms examples. If you are already familiar with C#, you can jump directly to one of the following SMS projects.

C# sms examples:

How to send sms from C#
How to send multiple sms from C#
How to send scheduled SMS from C#
How to receive sms in C#
How to delete SMS using C#
Download the latest C# sms api library from Github

What is C#

C# is a programming language. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that, programming languages have a more rigorous structure, to help the computer understand it better.

What is Visual Studio

Visual studio is a tool to write a C# program. Visual studio allows you to type in text using the C# language, and it makes it possible for you to tell the computer, to read the text and execute the instructions. We use the term "Run" to tell computer to execute the instructions.

What is a C# Hello world program

The C# hello world program is the most simple program you can write. It simply prints out the sententce: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in C#

To write your first program in C#:

  1. Setup Visual Studio Community
  2. Create a new C# Console Application
  3. Configure your project's name and directory
  4. Select the .NET Core for Target Framework
  5. Write your first 'Hello world' program in C#
  6. Run the C# 'Hello world' program
  7. Examine the error report
  8. If there is an error, fix it

Prerequisites

Here is what you need to get started. To write your first computer program in C#, you need a Windows computer, the Visual Studio programming environment and example code presented below.

  • Windows 10 computer
  • Microsoft Visual Studio Community Edition
  • Ozeki Hello World example project

Download Visual Studio

Microsoft Visual Studio Community Edition

In this video tutorial you will find how to download the Visual Studio Community Edition installer (Video 1). You may download the installer from the following URL: https://visualstudio.microsoft.com/downloads/. On this page you will see different version of Visual Studio available for download. Follow the instructions in this short video to download the installer on to your computer.

Video 1 - Download Visual Studio

Download Visual Studio installer

Start by going to the following website: https://visualstudio.microsoft.com/downloads/. Under the Community column, click on the purple Free download button (Figure 1). This is going to download the Microsoft Visual Studio Community Edition Installer on to your computer to the default download location specified by your browser.

download visual studio community
Figure 1 - Download Visual Studio Community

Install Visual Studio

To write your first C# program, you need to install visual studio. The following video shows you how this installation can be done.

Video 2 - Install Visual Studio

Create your first visual studio project

After visual studio has been installed, you need to open it, and create a console project. The console project is the most simple project you can create.

Video 3 - Create a console project

Create a new project in Visual Studio

First, open Visual Studio, which you have just installed. You will be then greeted by a welcome screen. This screen lets you create a new project or open previous ones. Under the "Get Started" column on the right, click on Create a new project (Figure 2). This will forward you to the next page where you can configure the settings of your project.

creating a new project in visual studio
Figure 2 - Create a new project in Visual Studio

Select Console Application in C#

In order to create an C# project, you will first need to filter the project to C# and Console Application. After you have clicked on "Create a new project" you will be brought to a configuration screen. First, select C# from the combobox in the top middle section of the window just below the search bar (Figure 3). Next, select Console in the combobox farthest to the right side of the window. As a result of your filtering an option titled Console Application will pop up. Select Console Application by clicking on it. Finally, click the Next button on the bottom right to continue.

console application in visual studio
Figure 3 - Select Console Application in C#

Name your application

After specifying your project as a Console Application, you will be asked to give it a name. Under the text that reads "Project name" you will find a textbox (Figure 4). Enter your desired name into this textbox to name your project. Give it an easily identifyable name, so you could always recognize it. We also recommend making sure that you place the solution and the project into the same library. This is done by clicking on the checkbox on the bottom of the section to enable this option. Click on the Next button on the bottom right when you are done.

naming a project in visual studio
Figure 4 - Give a good fitting name to your project

Select targeted framework

To finalize your project configuration you need to select a targeted framework. This can be done on the last configuration page. You can select a targeted framework from the combobox by clicking on it (Figure 5). Here we select .NET Core. This is a good choice for your project. Click on the Create button on the bottom right to create your project.

selecting dotnet version in visual studio
Figure 5 - Select the .NET version for your project

Write your 'hello world' program in C#

The first program you write in any programming language is the 'Hello world' program. The sole aim of this program is to print the term 'Hello world' to the computer screen. In this example the program consists of two lines of code: The first line: Console.WriteLine prints the text. The second line: Console.ReadLine waits for the user to hit enter (Code 1).

using System;

namespace MyFistConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is my hello world!");
        }
    }
}
	

Code 1 - Printing "This is my hello world!" to the console in C#

Write the Hello World code

This short video tutorial shows you how to write the code for the "Hello world" program in C#. This is done through the text editor in Visual Studio (Video 4). First, write the command Console.WriteLine() into main. Then the text goes into the parentheses and inside of quotation marks. The final line should look like this: Console.WriteLine("This is my hello world!").

Video 4 - Type in the Hello World source code

Run the C# "hello world" program

To run the hello world program, you need to click on the green Start button in the Visual Studio toolbar. You may also use the F5 key to run your program. Note that when you press F5, visual studio will first save your newly written file, then it will copmile it to an executable code, and then it will run it on your computer.

Video 5 - Run the Hello World program

Output of the code

After you have instructed Visual Studio to run you program a debug console will pop up. In this debug console you can see your program run in real time. This looks very much like a regular Windows command prompt. The first line you see is the output of your code: "This is my hello world!" (Figure 6). Then, 3 lines further down, you will see "exited with code 0". This means your program has closed.

console output in c sharp
Figure 6 - The console output of the program

What happens if I make a mistake in C#

If you make a mistake, when you write your instructions in C#, you will get a Syntax error. The computer will highlight the line with error in red, and it will tell you why didn't it understand the instructions. To correct the mistake you need to go back to the text editor and modify the program. Programs say they "fix the error" when they correct mistakes.

What is syntax error?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in C# and the computer does not understand what you say, he will reply with "Syntax error".

How to handle a syntax error in C#?

In the video below (Video 6) we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Video 6 - How to make an error and fix it

Error highlighting in Visual Studio

When you make a mistake in Visual Studio you are notified of the error in multiple places. First, the text editor itself show you that you have made an error (Figure 7). This happens even before you run the program. On the bottom of the screen Visual Studio also display the possible causes of the error. Here we can see there is 1, because there is a number 1 next to X on a red circle symbol.

missing comma in visual studio
Figure 7 - Missing comma in the end of the line

Error dialog in Visual Studio

After you ran the program and it found an error, it will display an error dialogue. This asks you if you want to run the last successful build (That actually ran before), or not. You should press No and look for the cause of the problem (Figure 8). On the bottom of the screen Visual Studio also displays the possible causes of the error.

alert in visual studio
Figure 8 - The IDE alerts you, that the code has errors in it

Error report in Visual Studio

If there is an error in your code, you can click on the X on a red circle symbol on the bottom. (Figure 9). This brings up the error report, which will list all the possible things that could have cause the problem. Here we can see there is one possibility listed. The report will give you a description of the problem, and on what line it occured on. This way it is easier for the programmer to identify the error and fix it.

error report in visual studio
Figure 9 - Error report in Visual Studio

Summary

Now that you have completed the five simple steps above, you have made your first step in becoming a C#.Net software developer. This program may seem simple, and it may not do much, but this is how great things start. Every programmer starts at this point, so do not be affraid, continue your stuides and the hard work will pay off.

Learning C# is advised if you want to develop web applications, web services and desktop applications. You can learn a lot about Object Oriented Programming and Functional Programming while you use C#. The next step in your learning journey will be creating an HTTP request.

Continue your reading in Ozeki's website, where you can find guides about sending and receiving SMS in C#.

Now download Ozeki SMS Gateway for the next part of C# programming and let the work begin!

More information