Fundamentals of C# Programming (Part One): The Concepts That Make Your “Hello World” a Reality

Martins charles
11 min readSep 3, 2023

--

By Charles Martins

C sharp programming language

We have established what .NET Core is in my last article: Getting Started with .NET Core. As I mentioned in that article, .NET Core supports languages like C#, Visual Basic, and F#. These languages are used to build applications for different industries across the world.

C# still remains one of the more popular languages, with around 6.8 million users worldwide. Today, I will be explaining fundamental concepts in C# (pronounced C-sharp), and this article will cover the basics. In this article, you will learn the following:

History of C#, its features and how it works

Writing your first C# code

Syntax of C#

Datatypes, Variables and Operators

History of C Sharp Programming

Anders Hejlsberg
Anders Hejlsberg

It might interest you to know that C# was derived from an earlier programming language called C. Most people often ask, are C# and C++ similar? The answer is, not entirely. Some may describe them as similar, but that is only because they share the same syntax. However, their features and how they compile are very different.

For instance, C# is compiled into intermediate-level language code by Roslyn, which can then be processed by the JIT (Just-in-time) compiler into machine code. C# is considered a high-level language. C++, on the other hand, is an intermediate-level language. What this means is that it comprises both high-level and low-level codes, and is compiled directly into machine codes.

C# was invented by Anders Heijsberg ( the man in the picture above). C# was developed in 2000, to tackle the increasing demand for web applications. Although C++ and Visual Basic existed for this purpose, there was an issue with building high-performance software using these languages. Therefore, C# was invented to tackle this problem.

Originally, when C# was introduced along with the .NET framework in 2002 it was close-source at the time. A decade later, Microsoft released Visual Studio Code, Roslyn, and the Unifed .NET platform, which support C# and are open-source.

Several versions of C# have emerged over the years, with the latest version being C# 11.0.

Features of C Sharp programming and how it works

Utilization of C# programming

What is C# programming language?

C# is an object-oriented programming language. This means C# belongs to a paradigm of programming languages that relies on the concepts of classes and objects. There are other paradigms of programming languages like procedural programming, structured programming, functional programming, etc. OOP languages are used to structure software into reusable blocks of code called classes that can be used to create individual instances of an object.

OOP languages like C# are efficient for designing and maintaining software for the following reasons: reusability, encapsulation, inheritance, polymorphism, and modularity. I will explain all of these concepts later in this article.

C# is a general-purpose and high-level language. This allows it to be used on different platforms and operating systems to perform a wide range of tasks. Although C# can perform a wide range of tasks, there are three ways in which it is most utilized:

  1. Building Web applications and dynamic websites
  2. Creating applications that are tailored specifically to Windows (Microsoft’s OS)
  3. Creating games, as it integrates well with the Unity Game Engine to create a splendid environment for game development.

C# is also a type-safe language. This means that types can only interact through the protocols they define. In simpler terms, once a variable is declared as a certain type, say int x (an integer), you cannot assign it to some string, like x = boy.

How C# works

C# runs on the .NET platform. And like most programming languages on the .NET platform, it is executed by the virtual execution system called the common language runtime (CLR). Roslyn (a compiler) compiles C# source codes into IL (intermediate language). The IL conforms to the CLR (Common Language Runtime) in the .NET Core platform. The IL is converted to machine code by the CLR with JIT (Just-in-time) compilation.

Other services that the CLR provides include automatic garbage collection, exception handling, and resource management

Writing your first C Sharp code in Visual Code studio

Coding in C Sharp

Install Visual Studio Code on your system, if you have not already.

Open a new folder on your VSCode and save it with something like ‘Coding with C#’. If you do not know how to do this, you can use this tutorial to see how to create a new folder in vscode.

Visual Code setup.
This is what your visual studio code set up should look like

I will also outline the steps here for creating a new folder if you do not wish to watch the tutorial:

> Open VS code

> Click on File in the top right corner, and a drop-down menu appears

> Click on Open folder (ctrl + k ctrl + o), and a small prompt box appears

> Create a new folder, and name it (e.g. Coding with C#)

creating a new folder
name your new folder

There, you have created a new folder, and you are within that folder.

Next, you need to launch the .NET Core platform in your visual code. You need to first download the two extensions: .NET runtime install tool and C# Dev Kit.

> Go to extensions on your Visual Studio code

> Enter the extensions you wish to download into the search bar (C# Dev Kit and .NET runtime install tool).

> Download the extensions

C# Dev Kit on Visual Studio Code

Once the extensions have been downloaded, it is time to create a new console in Visual Studio Code. You can do this via the terminal in these quick steps:

Open a terminal in VS code

> Open a new terminal

> Type dotnet new console into the terminal and press enter, this automatically launches a new console app for you

> Type dotnet build into the terminal and press enter, this builds the project and its dependencies

> You will see that your dependencies and the file program.cs have been created.

> Open the program.cs file, and already there is a code to print your first ‘Hello World’

Console.WriteLine (“Hello World!”);

> Type dotnet run into the terminal and press enter, and you will the message displayed.

If you are using an online IDE like Replit, you will see something more like the images displayed below

replit hello world
Your Hello World code
The console output

Note: When writing C# code, you need to understand that these commands are case-sensitive. For example:

console.writeline (“Hello World”);

This will give you an error message. You also need to make sure that you write the correct command.

Console.RightLine (“Hello World”);

You will also get an instant error message.

Now, we have written our first C# code. I would like to explain certain concepts, and this will help you understand the C# syntax better when you code.

Syntax of C Sharp Programming

Look at the block of code below—the same one that helped us print ‘Hello world’. I will explain what each line means and why it is important to the C# syntax.

using System;

class Program {
public static void Main (string[] args) {
Console.WriteLine (“Hello World”);
}
}

Line 1: using System, C# has predefined libraries that contain important classes and functions like Console. using System allows us access to these libraries. If you are coding using .NET 5 and above you may not always need to declare this.

Line 2: An empty white space. This is purely for aesthetic purposes, as C# ignores whitespace. However, adding white spaces to your code makes it easy to read.

Line 3: class Program is used to define a class called program that will contain all the data and methods that bring functionality to your program.

Line 3: Curly braces ({}) mark the beginning and the end of a block of code. The curly braces begin in Line 3 and end in Line 7.

Line 4: public static void Main are all keywords that you do not have to necessarily understand, this is just an entry point into the C# application. (string[] args) means the Main method is taking an argument for a string. Curly braces signify the block of code, starting in line 4 and ending in Line 6.

Line 5: Console.WriteLine, Console is a class of System. It has a method called WriteLine. The dot is an operator that allows us to access the method (WriteLine) of the class (Console). This statement takes an argument which is a string Hello world. Strings must be encased in double quotation marks (“”). Console.WriteLine (“Hello World”) is a statement, and every statement must end with a semicolon.

Line 6 & 7: These are closing curly braces. It is very important to close your code blocks to avoid errors.

Note: Statements are grouped into methods, methods into classes, and classes into namespaces. Keywords have special meaning to the compiler as they are already predefined.

Datatypes, Variables and Operators

For the sake of this article’s length, we will only briefly highlight these concepts. A more detailed article may surface in the future.

Datatypes

Types of Data in C#

C# is a strongly typed language. You must declare the type of data that a valid C# variable can hold. C# has a unified type system, also called the common type system. This means that all data types supported, including primitives like integers, are subclasses of the System.Object class.

Data types belonging to the common type system are divided into two categories: reference types and value types.

Value types: This data type holds the data within its own memory allocation. Boolean, Char, Date, enumerations, structures, and all numeric data types belong to this category.

You can declare it by using the reserved keyword for that type. 3 is a prime number.

int primeNumber = 3

Value types are auto-initialized after they have been declared. Value types have default values, since they cannot exist without data. The image below shows the default values for certain data types

Reference types: This data type stores references to the data, but does not contain the data. It stores the address where the data is stored. They can be predefined or user-defined.

Reference types include strings, arrays, interfaces, delegates, and classes.

Reference types unlike value types need to be intialized after they have been declared. This is you need to use the new keyword to create objects that will be referenced. For example:

className ObjectName = new className();

The default value of any reference type is null, as it must lacks an instance.

Below is a table of all the major C# datatypes and what they represent:

Variables

If you wish to store a data value of any type, a variable is needed. A variable is a container (or identifier) used to store data values. It is a named storage location in a computer’s memory.

The name and type of the variable should be declared before assigning it a data value in C#. This is why C# is referred to as a statically typed programming language.

Although, you can instruct the compiler to infer the type of variable from the expression using the var keyword. For instance,

var i = 5

i is compiled as an integer.

This is the basic syntax for declaring a variable in C#

<data type> <variable name> = <value>

int score = 20

I have declared a variable called score whose datatype is an integer (integer is a primitive value type). The equals sign (=)is an operator that allows us to assign data values to variables. Keywords like int, char and string define these variables. For instance, variables with keywords like int:

int: can store integers (whole numbers), without decimals, such as 1234 or -1234

double: can store floating point numbers, with decimals, such as 12.34 or -12.34

char: can store single characters, such as ‘a’ or ‘d’. Char values are encased by single quotes.

bool: can store values with two states: true or false.

string: can store text, such as “Hello World”. Unlike Char, string values are encased by double quotes.

There are different types of variables in C#. We will not dive into them, but you should know the basics. Click on each one to learn more about each type of variable in detail.

  1. Local Variables
  2. Instance or non-static variables
  3. Static or Class variables
  4. Constant Variables
  5. Read-only Variables

Operators

Operators are symbols or characters that perform mathematical or logical actions on operands. Operators are used to manipulate values in a program. For example:

x + y

Operands, x and y, are made to perform the mathematical action called addition when we use the arithmetic operator (+).

C# supports a wide range of operators. They are grouped into classes based on the actions they perform. There are, however, three broad classes: unary, binary, and ternary.

Urinary operators: It needs only one operand and an operator. For example, increasing (++) the value of a variable (a single operand).

i++

Binary operators: It needs at least two operands and one operator. For instance, the assignment (=) of a value to a variable (two operands).

string name = “Charles”

Ternary operators: This is used in conditional statements, like if-else statements, to check if a condition is true (?) or false (:) or compare objects. For example, given two values, x and y, which value is greater?

var result = x > y? “x is greater than y” : “y is greater than x” ;

So far, I have not discussed Statements and Arrays, which means there will be a second part to this article. But I do hope you enjoyed this first bit. You can follow my medium page to see more articles like this from me. You can also join my C# programming fundamentals list to see more articles on C#.

Next up, Fundamentals of C# Programming (Part One): Digging deeper with Statements and Arrays.

REFERENCES

Charles, M. (2023). Getting Started with dotNET Core: Fundamentals and Setup. Available at: https://medium.com/@martinscharles561/getting-started-with-dotnet-core-fundamentals-and-setup-a5ba7676b16

C# Data Types. Available at: https://www.w3schools.com/cs/cs_data_types.php.

C# Development Company | BairesDev. Available at: https://www.bairesdev.com/technologies/c-sharp/#:~:text=Developed%20by%20Microsoft%20in%202000.

C# programming with Visual Studio Code. [online] Available at: https://code.visualstudio.com/docs/languages/csharp.

C# Variables. [online] Available at: https://www.w3schools.com/cs/cs_variables.php.

Doherty, E. (2020). What is Object Oriented Programming? OOP Explained in Depth. Educative: Interactive Courses for Software Developers Available at: https://www.educative.io/blog/object-oriented-programming.

History of C#. Available at: https://www.w3schools.in/csharp/history-of-csharp.

Learn how it works — Training. Available at: https://learn.microsoft.com/en-us/training/modules/csharp-write-first/3-how-it-works

Learning, G. (2022). C# vs C++: Differences and Similarities Between C# & C++. Available at: https://www.mygreatlearning.com/blog/c-sharp-vs-cpp/#:~:text=Both%20C%2B%2B%20and%20C%23%20have

‌‌‌

--

--

Martins charles

Hi I'm Charles. A life long learner, welcome to my thought box, if you stay long enough I have exciting things to share!