Learn C#
Learn C#
What is C#?
C# is pronounced "C-Sharp".
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# 12, was released in
November 2023.
Introduction to the C# programming
language
C# is used for:
• Mobile applications
• Desktop applications
• Websites
• Command-line programs
• Cloud-based systems
• Games
• IoT devices
• Database applications
• Machine learning applications
Introduction to the C# programming
language
• C# first appeared in 2000 with the introduction of the .NET framework 1.0. For
almost two decades, developers have been using C# to develop applications on
Windows.
• In June 2016, Microsoft released .NET Core 1.0, a cross-platform version
of .NET, allowing you to develop applications that run across platforms, including
Windows, macOS, and Linux.
Why Use C#?
The .NET framework can work only on Windows. If you develop new
applications, you should not use the .NET framework.
.NET Mono (2)
The open-source community developed an implementation of the .NET
framework called the Mono project. Mono is open and cross-platform.
However, it’s often behind the official implementation of the .NET
framework.
• Xamarin mobile platform
• Unity game development platform
.NET Core (2)
Microsoft developed a specific version of the .NET framework that works
cross-platform called .NET Core. The .NET Core includes the cross-
platform implementation of the .NET framework, including:
OPERATOR IN C#
The following flowchart illustrates how the C# if statement works:
OPERATOR IN C#
The following flowchart illustrates how the C# if statement works:
OPERATOR IN C# -
LOGICAL
The following flowchart illustrates how the C# if statement works:
OPERATOR IN C#
Variables & types
C# Variables
Variables are identifiers whose values can change during the program’s execution.
When the program ends, the values stored in the variables are also gone.
Syntax:
type variableName;
The type can be any C# built-in type or custom type. For example, the int built-in type represents
the integers, and the string built-in type represents the text strings.
The variableName is a valid identifier that starts with a character or underscore (_) and is followed
by other characters.
Variables & types
Example declares a variable
Declaring a string
string message;
message = "Hi";
A string has the Length property that returns the length of a string. To access
the Length property, you use the dot operator (.)
Console.WriteLine(message);
C# Bool types
C# use the bool keyword to represent the boolean type with two values: true and false.
A variable of the bool type can hold one of these two values.
C# if statement
The if statement evaluates a condition and executes one or more statements if the result
is true. Otherwise, the if statement passes the control to the statement after it .
if (condition) if (expression) {
// statements
statement;
}
CONTROL FLOW
C# if statement
CONTROL FLOW
C# if statement
CONTROL FLOW
C# if statement
CONTROL FLOW
Nested C# if statement
The following flowchart illustrates how the C# if statement works:
C# if else
Execute a block when a condition is true and execute another block otherwise. In this case, you
need to use the if else statement.
if (condition) {
// if statements
}
else {
// else statements
}
The following flowchart illustrates how the C# if statement works:
C# if else
C# if else - Example
if (condition == "sunny")
{ Console.WriteLine("Let's go outside.");
}
else {
Console.WriteLine("Just stay home.");
}
Output:
Let's go outside.
Using if else statement with a complex condition
if (condition1) {
// block 1 }
else if (condition2) {
// block 2 }
else if (condition3) {
// block 3 }
else {
// else block
}
C# if else if
C# if else if - EXAMPLE
string dayName;
if (day == 1) {
dayName = "Sunday"; }
else if (day == 2) {
dayName = "Monday"; }
else if (day == 3) {
dayName = "Tuesday"; }
else if (day == 4) {
dayName = "Wednesday"; }
else if (day == 5) {
dayName = "Thursday"; }
else if (day == 6) {
dayName = "Friday"; }
else if (day == 7) {
dayName = "Saturday"; }
else { dayName = "Unknown";
}
C# switch
The switch statement evaluates an expression and selects a block for execution if the
expression satisfies a condition. The syntax of the switch statement is as follows:
switch (expression) {
case label1:
// block1;
break;
case label2:
// block2;
break;
case label3:
// block3;
break;
default:
// blockn;
break; }
C# switch
The switch statement evaluates an expression and selects a block for execution if the
expression satisfies a condition. The syntax of the switch statement is as follows:
switch (expression) {
case label1:
// block1;
break;
case label2:
// block2;
break;
case label3:
// block3;
break;
default:
// blockn;
break; }
C# switch
The switch statement evaluates an expression and selects a block for execution if the
expression satisfies a condition. The syntax of the switch statement is as follows:
switch (expression) {
case label1:
// block1;
break;
case label2:
// block2;
break;
case label3:
// block3;
break;
default:
// blockn;
break; }
C# while statement
C# while statement
C# while statement