2. WHY USE C#?
C# is pronounced "C-Sharp".
It is an object-oriented programming language
created by Microsoft that runs on the .NET
Framework.
C# has roots from the C family, and the language is
close to other popular languages like C++ and Java.
The first version was released in year 2002. The
latest version, C# 11, was released in November
2022.
3. WHY USE C#?
C# is used for:
• Mobile applications
• Desktop applications
• Web applications
• Web services
• Web sites
• Games
• VR
• Database applications
• And much, much more!
4. WHY USE C#?
• It is one of the most popular programming
language in the world
• It is easy to learn and simple to use
• It has a huge community support
• C# is an object oriented language which gives a
clear structure to programs and allows code to
be reused, lowering development costs
• As C# is close to C, C++ and Java, it makes it
easy for programmers to switch to C# or vice
versa
5. C# IDE
• The easiest way to get started with C#, is to
use an IDE.
• An IDE (Integrated Development Environment)
is used to edit and compile code.
• Applications written in C# use the .NET
Framework, so we will use Visual Studio, as the
program, the framework, and the language, are
all created by Microsoft.
7. 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
the class Program.
8. C# SYNTAX
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.
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.
9. C# SYNTAX
Note: Every C# statement ends with a
semicolon ;
Note: C# is case-sensitive: “MyClass” and
“myclass” has different meaning or use
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.
10. C# SYNTAX
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.
11. C# OUTPUT
To output values or print text in C#, you can use
the WriteLine() method:
To can add as many WriteLine() methods as you
want. Note that it will add a new line for each
method:
12. C# OUTPUT
You can also output numbers, and perform
mathematical calculations:
There is also Write() method, which is similar to
WriteLine().
The only difference is that it does not insert a new
line at the end of the output:
13. C# COMMENTS
Comments can be used to explain C# code, and
make it more readable. It can also be used to
prevent execution when testing alternative code.
SINGLE-LINE COMMENTS
Single-line comments start with two forward
slashes ( // ).
Any text between // and the end of the line is
ignored by C# (will not be executed).
14. C# COMMENTS
This example uses a single-line comment before
a line of code:
This example uses a single-line comment at the
end of a line of code:
15. C# COMMENTS
MULTI-LINE COMMENTS
• Multi-line comments start with /* and ends with
*/.
• Any text between /* and */ will be ignored by C#.
This Example uses a multi-line comment (a
comment block) to explain the code:
16. C# COMMENTS
Single or Multi-line comments?
It is up to you which one you want to use. Normally,
we use // for short comments, and /**/ for long
comments.
17. C# VARIABLES
Variables are containers for storing data values.
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 and False
18. C# VARIABLES
DECLARING (Creating) VARIABLES
To create a variable, you must specify the type and assign it
a value:
Where type is a C# Data 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.
19. C# VARIABLES
DECLARING (Creating) VARIABLES
To create a variable that should store text, look at the
following example:
Create a variable called name of type string and assign it
the value “John”:
20. C# VARIABLES
DECLARING (Creating) VARIABLES
To create a variable that should store number, look at the
following example:
Create a variable called myNum of type int and assign it the
value 15:
21. C# VARIABLES
DECLARING (Creating) VARIABLES
You can also declare a variable without assigning the value,
and assign the value later:
22. C# VARIABLES
DECLARING (Creating) VARIABLES
You can assign a new value to an existing variable, it will
overwrite the previous value:
Change the value of myNum to 20:
24. C# 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:
25. C# CONSTANTS
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.
26. C# DISPLAY VARIABLES
The WriteLine() method is often used to display variable
values to the console window:
To combine both text and a variable, use the + character:
28. C# DISPLAY VARIABLES
For numeric values, the + character works as a
mathematical operator (notice that we use int (integer)
variables here):
From the example above, you can expect:
x stores the value 5
y stores the value 6
Then we use the WriteLine() method to display the value of x
+ y, which is 11.
29. C# DECLARE MANY VARIABLES
To declare more than one variable of the same type, use a
comma-separated list:
You can also assign the same value to multiple variables in
one line:
30. C# IDENTIFIERS
All C# variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more
descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in order
to create understandable and maintainable code:
31. C# IDENTIFIERS
The general rules for naming variables are:
• Names can contain letters, digits and the underscore
character (_)
• Names must begin with a letter
• Names should start with a lowercase letter and it 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