C# Tutorial
C# Tutorial
C# Introduction
What is C#?
o C# is pronounced “C-Sharp”.
o It is an Object-oriented programming language created by Microsoft that runs on the .NET Framework.
o C# has roots from the C family, and the language is close to other popular languages like C++ and
Java.
o The first version was released in year 2002. The latest version, C# 12, was released in November
2023.
C# is used for:
Mobile applications
Web applications
Desktop applications
Web services
Websites
Games
VR
Database applications
C# IDE
Program.cs:
2
Don’t worry if you don’t understand the code above – we will discuss it in detail in later chapters. For now,
focus on how to run the code.
Run the program by pressing F5 button on your keyboard (or click on “Debug” -> “Start Debugging”).
This will compile and execute your code.
III. C# Syntax
Line 1: using System means that we can use classes from the System namespace.
Line 2: A blank line. C# ignores white space. However, 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.
Don’t worry if you don’t understand how using System, namespace, and class works. Just think of it as
something that almost always appears in your program, and that you will learn more about them in a later
chapter.
Line 7: Another thing that always appears 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 example, it will output “Hello World!”.
If you omit the using System line, you would have to write System.Console.WriteLine() to print/output
text.
Note: 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: Go to the
Get Started Chapter for how to install C#.
IV. C# Output
To output values or print text in C#, you can use the WriteLine() method:
Example:
You can add as many WriteLine() methods as you want. Note that it will add new line for each
method:
Example:
2
You can also output numbers, and perform mathematical calculations:
Example:
The only difference is that it does not insert a new line at the end of the output:
Example:
Note that we add an extra space when needed (after “Hello World!” in the example above), for better
readability.
In this tutorial, we will only use WriteLine() as it makes it easier to read the output of the code.
V. C# Comments
Comments can be used to explain C# code, and to make it more readable. It can also be used to prevent
execution when testing alternative code.
C# Single-line Comments
Any text between // and the end of the line is ignored by C# (will not be executed).
Example:
Example:
2
C# Multi-line Comments
This example uses a multi-line comment (a comment block) to explain the code:
Example:
In C#, there are different types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes.
string - stores text, such as "Hello World". String values are surrounded by double quotes.
bool - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
Syntax
Where type is a C# type (such as int or string), and variableName is the name of the variable (such
as x or name). The equal sign is used to assign values to the variable.
To create a variable that should store text, look at the following example:
Example:
Create a variable name called name of type string and assign it the value “John”:
To create a variable that should store a number, look at the following example:
Example:
Create a variable called myNum of type int and assign it the value 15:
You can also declare a variable without assigning the value, and assign the value later:
Example:
2
Note that if you assign a new value to an existing variable, it will overwrite the previous value:
Example:
Other Types
Example:
C# Variables (Constants)
If you don't want others (or yourself) to overwrite existing values, you can add the const keyword
in front of the variable type.
This will declare the variable as "constant", which means unchangeable and read-only:
Example:
The const keyword is useful when you want a variable to always store the same value, so that others (or
yourself) won’t mess up your code. An example that is often referred to as a constant is PI (3.14159…)
Note: You cannot declare a constant variable without assigning the value. If you do, an error will occur:
A const field requires a value to be provided.
The WriteLine() method is often used to display variable values to the console window.
To combine both text and a variable, use the + character:
Example:
2
You can also use the + character to add a variable to another variable:
Example:
For numeric values, the + character works as a mathematical operator (notice that we use int (integer)
variables here):
Example:
To declare more than one variable of the same type, use a comma-separated list:
Example:
You can also assign the same value to multiple variables in one line:
Example:
C# Variables (Identifiers)
2
All C# variables must be identified with unique names.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names to create understandable and maintainable code:
Example:
Names can contain letters, digits and the underscore character (_)
Names must begin with a letter or underscore.
Names should start with a lowercase letter and cannot contain whitespace.
Names are case-sensitive ("myVar" and "myvar" are different variables)
Reserved words (like C# keywords, such as int or double) cannot be used as names.