Introducing C# and .
NET
Abdulmottaleb Elabour
1
Introducing C# and .NET
• C# is a general-purpose, type-safe, object-oriented programming language.
• The goal of the language is programmer productivity. To this end, C# balances
simplicity,expressiveness, and performance.
• The chief architect of the language since its first version is Anders Hejlsberg (creator
of Turbo Pascal and architect of Delphi).
• The C# language is platform neutral and works with a range of platform-specific
runtimes.
2
Object Orientation
C# is a rich implementation of the object-orientation paradigm, which includes
encapsulation, inheritance, and polymorphism. Encapsulation means creating a
boundary around an object to separate its external (public) behavior from its internal
(private) implementation details.
3
Unified type system
The fundamental building block in C# is an encapsulated unit of data and functions
called a type. C# has a unified type system in which all types ultimately
share a common base type. This means that all types, whether they represent
business objects or are primitive types such as numbers, share the same basic
functionality. For example, an instance of any type can be converted to a string
by calling its ToString method.
4
Classes and interfaces
In a traditional object-oriented paradigm, the only kind of type is a class. In C#,
there are several other kinds of types, one of which is an interface. An interface
is like a class that cannot hold data. This means that it can define only behavior
(and not state), which allows for multiple inheritance as well as a separation
between specification and implementation.
5
Properties, methods, and events
In the pure object-oriented paradigm, all functions are methods. In C#, methods
are only one kind of function member, which also includes properties and
events (there are others, too). Properties are function members that encapsulate
a piece of an object’s state such as a button’s color or a label’s text. Events are
function members that simplify acting on object state changes.
6
Type Safety
C# is fundamentally a type-safe language, meaning that types can only interact through
their defined protocols, which helps maintain the integrity of each type. For example,
C# prevents the use of a string type as if it were an integer type.
Static typing helps avoid many errors before code execution by shifting the burden from
runtime unit tests to the compiler, which verifies that all types in a program are
compatible. This reduces complexity in managing large programs, making them more
predictable and robust.
7
Memory Management
C# relies on the runtime to perform automatic memory management. The Common
Language Runtime has a garbage collector that executes as part of your
program, reclaiming memory for objects that are no longer referenced. This frees
programmers from explicitly deallocating the memory for an object, eliminating the
problem of incorrect pointers encountered in languages such as C++.
8
Platform Support
C# has runtimes that support the following platforms:
• Windows 7+ Desktop (for rich-client, web, server, and command-line
applications)
• macOS (for web and command-line applications—and rich-client applications
via Mac Catalyst)
• Linux (for web and command-line applications)
• Android and iOS (for mobile applications)
• Windows 10 devices (Xbox, Surface Hub, and HoloLens) via UWP
There is also a technology called Blazor that can compile C# to web assembly that
runs in a browser.
9
CLRs, BCLs, and Runtimes
Runtime support for C# programs consists of a Common Language Runtime and
a Base Class Library. A runtime can also include a higher-level application layer
that contains libraries for developing rich-client, mobile, or web applications.
Different runtimes exist to allow for different kinds of applications, as
well as different platforms.
10
Common Language Runtime
• Common Language Runtime (CLR) provides essential runtime services such as
automatic memory management and exception handling.
• (The word “common” refers to the fact that the same runtime can be shared by other
managed programming languages, such as F#, Visual Basic, and Managed C++.)
• C# is called a managed language because it compiles source code into managed
code, which is represented in Intermediate Language (IL).
• The CLR converts the IL into the native code of the machine, such as X64 or X86,
usually just prior to execution. This is referred to as Just-In-Time (JIT) compilation.
11
Base Class Library
A CLR always ships with a set of assemblies called a Base Class Library (BCL).
A BCL provides core functionality to programmers, such as collections, input/output,
text processing, XML/JSON handling, networking, encryption, interop, concurrency,
and parallel programming.
A BCL also implements types that the C# language itself requires (for features such
as enumeration, querying, and asynchrony) and lets you explicitly access features of
the CLR, such as Reflection and memory management.
12
.NET Framework
.NET Framework is Microsoft’s original Windows-only runtime for writing web and
rich-client applications that run (only) on Windows desktop/server. No major new
releases are planned, although Microsoft will continue to support and maintain the
current 4.8 release due to the wealth of existing applications.
With the .NET Framework, the CLR/BCL is integrated with the application
layer. Applications written in .NET Framework can be recompiled under .NET 8,
although they usually require some modification. Some features of .NET Framework
are not present in .NET 8 (and vice versa).
13
C# Language
• 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# 13, was released in November 2024.
C# is used for:
• Mobile applications
• Desktop applications
• Web applications
• Web services
• Web sites
• Games
• VR
• Database applications
• And much, much more!
14
Identifiers and Keywords
• Identifiers are names that programmers choose for their classes, methods, variables,
and so on.
• An identifier must be a whole word, essentially made up of Unicode characters
starting with a letter or underscore.
• C# identifiers are case sensitive. By convention, parameters, local variables, and
private fields should be in camel case (e.g., myVariable), and all other identifiers
should be in Pascal case (e.g., MyMethod).
• Keywords are names that mean something special to the compiler.
• Most keywords are reserved, which means that you can’t use them as identifiers.
15
Keywords
Here is the full list of C# reserved keywords:
16
Keywords
If you really want to use an identifier that clashes with a reserved keyword, you can
do so by qualifying it with the @ prefix. For instance:
int using = 123; // Illegal
int @using = 123; // Legal
The @ symbol doesn’t form part of the identifier itself. So, @myVariable is the same
as myVariable.
17
Contextual keywords
Some keywords are contextual, meaning that you also can use them as identifiers—
without an @ symbol:
18
Types and Conversions
C# can convert between instances of compatible types. A conversion always creates
a new value from an existing one. Conversions can be either implicit or explicit:
implicit conversions happen automatically, and explicit conversions require a cast.
In the following example, we implicitly convert an int to a long type (which has
twice the bit capacity of an int) and explicitly cast an int to a short type (which
has half the bit capacity of an int):
19
Types and Conversions
Implicit conversions are allowed when both of the following are true:
• The compiler can guarantee that they will always succeed.
• No information is lost in conversion
Conversely, explicit conversions are required when one of the following is true:
• The compiler cannot guarantee that they will always succeed.
• Information might be lost during conversion.
20
Value Types Versus Reference Types
All C# types fall into the following categories:
• Value types
• Reference types
• Generic type parameters
• Pointer types
21
Value Types Versus Reference Types
• Value types comprise most built-in types (specifically, all numeric types, the char type,
and the bool type) as well as custom struct and enum types.
• Reference types comprise all class, array, delegate, and interface types. (This includes
• the predefined string type.)
• The fundamental difference between value types and reference types is how they are
• handled in memory.
22
Value types
The content of a value type variable or constant is simply a value. For example, the
content of the built-in value type, int, is 32 bits of data. You can define a custom value type with the struct
keyword :
p1 and p2 have independent
storage.
23
Reference types
A reference type is more complex than a value type, having two parts: an object and the reference to that
object. The content of a reference-type variable or constant is a reference to an object that contains the
value. Here is the Point type from our previous example rewritten as a class rather than a struct.
24
Predefined Type Taxonomy
sbyte short int long
byte ushort uint ulong
float double decimal
bool
char
string
object
25
Predefined Type Taxonomy
Predefined types in C# alias .NET types in the System namespace. There
is only a syntactic difference between these two statements:
26
Numeric Types
27
Numeric Literals
Integral-type literals can use decimal or hexadecimal notation; hexadecimal is denoted
with the 0x prefix. For example:
You can insert an underscore anywhere within a numeric literal to make it more readable:
28
Numeric suffixes
Numeric suffixes explicitly define the type of a literal, These suffixes help avoid implicit
conversions and make the code more readable. Suffixes can be either lowercase
or uppercase, and are as follows:
Avoid using lowercase l because it looks similar to 1. 29
Converting between integral types
Integral type conversions are implicit when the destination type can represent every
possible value of the source type. Otherwise, an explicit conversion is required; for
example:
30
Converting between floating-point types
31
Decimal conversions
32
Arithmetic Operators
33
Conditional Operators
The && and || operators test for and and or conditions. They are frequently used in conjunction with the !
operator, which expresses not. In the following example, the UseUmbrella method returns true if it’s rainy
or sunny (to protect us from the rain or the sun), as long as it’s not also windy (umbrellas are useless in the
wind):
34
Conditional operator (ternary operator)
The conditional operator (more commonly called the ternary operator because it’s
the only operator that takes three operands) has the form q ? a : b; thus, if
condition q is true, a is evaluated; otherwise b is evaluated:
35
Arrays
An array represents a fixed number of variables (called elements) of a particular type. The
elements in an array are always stored in a contiguous block of memory, providing highly
efficient access. An array is denoted with square brackets after the element type:
This prints “e” because array indexes start at 0.
36
Arrays
You can use a for loop statement to iterate through each element in the array. The for loop
in this example cycles the integer i from 0 to 4:
37
Arrays
An array initialization expression lets you declare and populate an array in a single
step:
Or simply:
38
Multidimensional Arrays
Multidimensional arrays come in two varieties: rectangular and jagged. Rectangular
arrays represent an n-dimensional block of memory, and jagged arrays are arrays of
arrays.
39
Jagged arrays
Jagged arrays are declared using successive square brackets to represent each
dimension. Here is an example of declaring a jagged two-dimensional array for
which the outermost dimension is 3:
40
Jagged arrays
You can initialize a jagged array with explicit values. The following code creates an
array identical to the previous example with an additional element at the end:
41
The ref modifier
To pass by reference, C#
provides the ref parameter
modifier. In the following
example, p and x refer to the
same memory locations:
42
The out modifier
An out argument is like a ref argument except for the following:
• It need not be assigned before going into the function.
• It must be assigned before it comes out of the function.
The out modifier is most commonly used to get multiple return values back from a
Method.
43
The out modifier
Like a ref parameter, an out parameter is passed by reference.
44
Operators
45
Null Operators
C# provides three operators to make it easier to work with nulls:
• the null-coalescing operator
• the null-coalescing assignment operator
• the null-conditional operator.
46
Null-Coalescing Operator
The ?? operator is the null-coalescing operator. It says, “If the operand to the left is
non-null, give it to me; otherwise, give me another value.” For example:
47
Null-Coalescing Assignment Operator
The ??= operator (introduced in C# 8) is the null-coalescing assignment operator. It
says, “If the operand to the left is null, assign the right operand to the left operand.”
Consider the following:
48
Null-Conditional Operator
The ?. operator is the null-conditional or “Elvis” operator. It allows you to call methods and access members
just like the standard dot operator except that if the operand on the left is null, the expression evaluates to
null instead of throwing a NullReferenceException:
49