0% found this document useful (0 votes)
60 views26 pages

Chapter Two

The document is an introductory chapter on C# programming that discusses the fundamentals of C# programs including variables, data types, expressions, and control flow statements. It describes what variables are and their characteristics like name, type, and value. It also explains the basic C# data types such as integer, floating-point, Boolean, character, and string types as well as object and nullable types.

Uploaded by

Abenezer Teshome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views26 pages

Chapter Two

The document is an introductory chapter on C# programming that discusses the fundamentals of C# programs including variables, data types, expressions, and control flow statements. It describes what variables are and their characteristics like name, type, and value. It also explains the basic C# data types such as integer, floating-point, Boolean, character, and string types as well as object and nullable types.

Uploaded by

Abenezer Teshome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter Two

Introducing C# Programming
By Haben B. @St.Mary’s University-2022

1
Content
Understanding the Fundamentals of a C# Program
Using C# Predefined Types
Writing Expressions
Creating Conditional Statements
Creating Iteration Statements

2
What Is a Variable?
A typical program uses various values that change during
its execution. For example, we create a program that
performs some calculations on the values entered by the
user.
The values entered by one user will obviously be different
from those entered in by another user. This means that when
creating the program, the programmer does not know what
values will be introduced as input, and that makes it
necessary to process all possible values a user may enter.
The values in this part of memory change (vary) throughout
execution and this has led to their name variables.

3
Data Types
Data Types
 Data types are sets (ranges) of values that have similar
characteristics. For instance byte type specifies the set of
integers in the range of [0-255].
Characteristics
 Data types are characterized by:
 Name – for example, int;
 Size (how much memory they use) – for example, 4 bytes;
 Default value – for example 0.

4
Types
Basic data types in C# are distributed into the following
types:
Integer types – sbyte, byte, short, ushort, int, uint,
long, ulong;
Real floating-point types – float, double;
Real type with decimal precision – decimal;
Boolean type – bool;
Character type – char;
String – string;
Object type – object.

5
 These data types are called primitive (built-in types), because they
are embedded in C# language at the lowest level. The table below
represents the above mentioned data types, their range and their default
values:

6
Correspondence between C# and .NET Types
Primitive data types in C# have a direct correspondence with the
types of the common type system (CTS) in .NET Framework.
For instance, int type in C# corresponds to System.Int32 type
in CTS and to Integer type in VB.NET language, while long
type in C# corresponds to System.Int64 type in CTS and to
Long type in VB.NET language.
Due to the common types system (CTS) in .NET Framework
there is compatibility between different programming languages
(like for instance, C#, Managed C++, VB.NET and F#).
For the same reason int, Int32 and System.Int32 types in C#
are actually different aliases for one and the same data type –
signed 32-bit integer.

7
Integer Types
 Integer types represent integer numbers and are sbyte,
byte, short, ushort, int, uint, long and ulong. Let’s
examine them one by one.

8
Real Floating-Point Types
 Real types in C# are the real numbers we know from mathematics. They are
represented by a floating-point according to the standard IEEE 754 and are
float and double. Let’s consider in details these two data types and
understand what their similarities and differences are.
Real Type Float
 The first type we will consider is the 32-bit real floating-point type float. It is
also known as a single precision real number. Its default value is 0.0f or 0.0F
(both are equivalent). The character 'f' when put at the end explicitly
indicates that the number is of type float (because by default all real numbers
are considered double).
 Special Values of the Real Types
The real data types have also several special values that are not real numbers but are
mathematical abstractions:
o - Negative infinity -∞ (Single.NegativeInfinity). It is obtained when for instance we
are dividing -1.0f by 0.0f.
o - Positive infinity +∞ (Single.PositiveInfinity). It is obtained when for instance we
are dividing 1.0f by 0.0f.
o - Uncertainty (Single.NaN) – means that an invalid operation is performed on real
numbers. It is obtained when for example we divide 0.0f by 0.0f, as well as when
9 calculating square root of a negative number.
Real Type Double
 The second real floating-point type in the C# language is the double type. It is also

called double precision real number and is a 64-bit type with a default value of 0.0d
and 0.0D (the suffix 'd' is not mandatory because by default all real numbers in C#
are of type double). This type has precision of 15 to 16 decimal digits. The range of
values, which can be recorded in double (rounded with precision of 15-16 significant
decimal digits), is from ±5.0x 10-324 to ±1.7 × 10308.
 The smallest real value of type double is the constant
Double.MinValue = -1.79769e+308 and
the largest is Double.MaxValue = 1.79769e+308.
 The closest to 0 positive number of type double is
Double.Epsilon = 4.94066e- 324.
 As with the type float the variables of type double can take the special values:
Double.PositiveInfinity (+∞), Double.NegativeInfinity (-∞) and Double.NaN (invalid
number).

10
Real Floating-Point Types – Example
Here is an example in which we declare variables of
real number types, assign
values to them and print them:

11
Boolean Type
 Boolean type is declared with the keyword bool. It has two possible values: true and
false. Its default value is false. It is used most often to store the calculation result of
logical expressions.
Boolean Type – Example

12
Character Type
 Character type is a single character (16-bit number of a Unicode
table character). It is declared in C# with the keyword char. The
Unicode table is a technological standard that represents any character
(letter, punctuation, etc.) from all human languages as writing systems
(all languages and alphabets) with an integer or a sequence of integers.
 Character Type – Example

13
Strings
Strings are sequences of characters. In C# they are
declared by the keyword string. Their default value is null.
Strings are enclosed in quotation marks. Various text
processing operations can be performed using strings:
concatenation (joining one string with another), splitting by
a given separator, searching, replacement of characters and
others.

14
Object Type
Object type is a special type, which is the parent of all
other types in the .NET Framework. Declared with the
keyword object, it can take values from any other
type. It is a reference type, i.e. an index (address) of a
memory area which stores the actual value.

15
Nullable Types
 Nullable types are specific wrappers around the value types (as int,
double and bool) that allow storing data with a null value. This provides
opportunity for types that generally do not allow lack of value (i.e. value
null) to be used as reference types and to accept both normal values and
the special one null. Thus nullable types hold an optional value.
 Wrapping a given type as nullable can be done in two ways:

 Both declarations are equivalent. The easiest way to perform this operation
is to add a question mark (?) after the type, for example int?,
 The more difficult is to use the Nullable<…> syntax.
 Nullable types are reference types i.e. they are reference to an object in the
dynamic memory, which contains their actual value. They may or may not
have a value and can be used as normal primitive data types, but with some
specifics, which are illustrated in the following example:
16
The example above shows how a nullable variable (int?) can have a
value directly added even if the value is non-nullable (int). The opposite
is not directly possible. For this purpose, the nullable types’ property
Value can be used.

17
Nullable Types(Con’t)

Nullable types are used for storing information, which


is not mandatory. For example, if we want to store
data for a student such as the first name and last name
as mandatory and age as not required, we can use type
int? for the age variable:

18
Variables
A variable is a container of information, which can change its value.
It provides means for:
 Storing information;
 Retrieving the stored information;
 Modifying the stored information.
Characteristics of Variables
Variables are characterized by:
 Name (identifier), for example age;
 Type (of the information preserved in them), for example int;
 Value (stored information), for example 25.
 A variable is a named area of memory, which stores a value from a
particular data type, and that area of memory is accessible in the
program by its name.

19
Variables(Con’t)
 Primitive data types (numbers, char, bool) are called value types because
they store their value directly in the program stack.
 Reference data types (such as strings, objects and arrays) are an address,
pointing to the dynamic memory where their value is stored. They can be
dynamically allocated and released i.e. their size is not fixed in advance
contrary to the case of value types.

20
Naming Variables – Rules
Variable names can contain the letters a-z, A-Z, the digits 0-9 as well as the
character '_'.
Variable names cannot start with a digit.
Variable names cannot coincide with a keyword of the C# language. For example,
base, char, default, int, object, this, null and many others cannot be used as
variable names.
Naming Variables – Examples
Proper names:
 name
 first_Name
 _name1
Improper names (will lead to compilation error):
 1 (digit)
 if (keyword)
 1name (starts with a digit)

21
Character Literals
Character literals are single characters enclosed in
apostrophes (single quotes). We use them to set the values of
type char. The value of a character literal can be:
 a character, for example 'A';
 a character code, for example '\u0065';
 an escaping sequence;

o \' – single quote


o \" – double quotes
o \\ – backslash
o \n – new line
o \t – offset (tab)
o \uXXXX – char specified by its Unicode number, for example \u03A7.

22
Operators and Expressions
Operators in C#
Operators in C# can be separated in several different categories:
 Arithmetic operators – they are used to perform simple
mathematical operations.
 Assignment operators – allow assigning values to variables.
 Comparison operators – allow comparison of two literals and/or
variables.
 Logical operators – operators that work with Boolean data types
and Boolean expressions.
 Binary operators – used to perform operations on the binary
representation of numerical data.
 Type conversion operators – allow conversion of data from one
type to another.

23
Operators
Operator Categories
Below is a list of the operators, separated into
categories:

24
Conditional Operator ?:
 The conditional operator ?: uses the Boolean value of an expression to
determine which of two other expressions must be calculated and
returned as a result. The operator works on three operands and that is
why it is called ternary operator. The character "?" is placed between
the first and second operand, and ":" is placed between the second and
third operand. The first operand (or expression) must be Boolean, and
the next two operands must be of the same type, such as numbers or
strings.
 The operator ?: has the following syntax:
operand1 ? operand2 : operand3

25
Operator "??"
The operator ?? is similar to the conditional
operator ?:. The difference is that it is placed between
two operands and returns the left operand only if its
value is not null, otherwise it returns the right operand.
Example:
operand1 ?? operand2

26

You might also like