0% found this document useful (0 votes)
11 views

Learn C#

The document provides an introduction to the C# programming language. C# is a general-purpose, type-safe programming language developed by Microsoft. It discusses the basics of C#, including its uses, why it should be used, and how to install Visual Studio. It also covers C# programming fundamentals like variables, data types, operators, and control flow statements.

Uploaded by

Alejandro Sotelo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Learn C#

The document provides an introduction to the C# programming language. C# is a general-purpose, type-safe programming language developed by Microsoft. It discusses the basics of C#, including its uses, why it should be used, and how to install Visual Studio. It also covers C# programming fundamentals like variables, data types, operators, and control flow statements.

Uploaded by

Alejandro Sotelo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

LEARN C#

Introduction to the C# programming


language
Introduction to the C# programming
language

C# (C-Sharp) is a programming language developed by


Microsoft that runs on the .NET Framework.
Introduction to the C# programming language
NET Framework is used to create and run software
applications. . NET apps can run on many operating systems, using
different implementations of . NET. . NET Framework is used for
running.
Introduction to the C# programming
language

What is C#?

The C# (/si ʃɑːrp/ or see sharp) is a general-purpose, type-safe, object-oriented programming


language developed by Microsoft.

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#?

•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
Understanding .NET

.NET framework, .NET Mono, .NET Core


are closely related and overlapping
platforms. It’s essential to understand each
of them and what they can do before
developing applications
.NET framework (2)

• Common language runtime (CLR) that manages the code


execution.
• Baseclass library (BCL) that provides a library to build
applications.

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:

• CoreCLR is the cross-platform implementation of CLR


• CoreFX is the cross-platform implementation of BCL
Installation of Visual Studio

Microsoft offers three versions of Visual Studio 2022


including community, professional, and enterprise.

You only need the community edition which is free for


students, open-source contributors, and individuals.
Installation of Visual Studio

On the workloads, select the following:

• .NET desktop development


• Universal Windows Platform development
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#
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

int age; double weight = 60.5;


age = 18 double height = 1.72;

int age = 18; double weight = 60.5,


age = age + 1; height = 1.72;

int age; double weight = 60.5,


age = "one"; height = 1.72;
bmi;
C# float types
C# float types

float rate = 5.2F;


float amount = 10_000.5f;

double dimension = 3.14;


double radius = 1_000.5;

decimal amount = 9.99m;


decimal tax = 0.08M;
C# String types
C# uses the string keyword to represent the string type. The string keyword is
an alias for the System.String type. Therefore, the string and String are
equivalent.

Declaring a string
string message;
message = "Hi";

string message = "Hi";

string message = String.Empty;

string message = "";


C# float types

Get the length of a string

A string has the Length property that returns the length of a string. To access
the Length property, you use the dot operator (.)

string message = "Hello";


Console.WriteLine(message.Length);
C# float types

Concatenation of two string


A string has the Length property that returns the length of a string. To access
the Length property, you use the dot operator (.)

string message = "Good" + " Morning";


Console.WriteLine(message);

string message = "Good“;


Message += " Morning !";

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.

bool canVote = true; bool result = "One" == "One";


bool open = false; Console.WriteLine(result);

bool result = 10 > 20;


Console.WriteLine(result);
CONTROL FLOW

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 .

syntax of the if statement:

if (condition) if (expression) {
// statements
statement;
}
CONTROL FLOW
C# if statement

The following flowchart illustrates how the C# if statement works:


CONTROL FLOW
C# if statement
The following flowchart illustrates how the C# if statement works:

CONTROL FLOW
C# if statement

if statement with condition evaluates to false


The following flowchart illustrates how the C# if statement works:

CONTROL FLOW
C# if statement

if statement example with a complex condition


The following flowchart illustrates how the C# if statement works:

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.

Syntax of 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

string condition = "sunny";

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

string condition = "sunny";


bool free = true;

if (free & condition == "sunny") {


Console.WriteLine("Let's go outside.");
} else
{
Console.WriteLine("Just stay home.");
}
Output:
Let's go outside.
C# if else if
Both the if and if else statements check only one condition.
Check multiple conditions and execute a block if a condition is true.

syntax of the if else if statement:

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

You might also like