Chapter 1
Objectives
Explain the need for C#
Discuss flow control of a C# program
Explain the fundamental data types in C#
Discuss the concept of Boxing and UnBoxing
Discuss Structures in C#
Discuss Enumeration types
Programming in C# /
Compile and run a C# program
Chapter 1/ 2 of 39
Microsoft .net
Represents a new platform for developing
windows & web applications.
Supports more than 20 languages.
Revolutionizes facilities available for Windows
programming.
Programming in C# /
Chapter 1/ 3 of 39
Introduction to C#
Takes full advantage of the .net Platform.
The most efficient compiler in the .net family.
A modern replacement for C++.
Enhances developer productivity and increases
safety, by enforcing script type checking.
Allows restricted use of native pointers.
Programming in C# /
Chapter 1/ 4 of 39
C# Program Flow
A simple C# program:
Programming in C# /
Chapter 1/ 5 of 39
C# Constructs (1)
Variables in C# are declared as follows:
Programming in C# /
Chapter 1/ 6 of 39
C# Constructs (2)
If you want to use any keyword as the
variable name, you have to prefix the
variable name with @.
Programming in C# /
Chapter 1/ 7 of 39
Default Values (1)
Variables in C# are automatically
assigned a
default value upon creation.
Output:
Programming in C# /
Chapter 1/ 8 of 39
Default Values (2)
The default values of the common data types:
Programming in C# /
Chapter 1/ 9 of 39
Input / Output In C# (1)
Uses methods of the Console class in the
System namespace.
The most widely used methods are:
Programming in C# /
Chapter 1/ 10 of 39
Input / Output In C# (2)
The highlighted line acts as a placeholder where
Programming
in C#will
/ be
the value of the specified variable
(result)
Chapter 1/ 11 of 39
displayed.
Input / Output In C# (3)
The program will accept a line from the user and
Programming in C# /
echo it back as output.
Chapter 1/ 12 of 39
The if construct
Performs conditional branching.
Syntax :
The expression always requires to
Programming in C# /
evaluate to
Chapter 1/ 13 of 39
an expression of Boolean type.
Selection Statement
The above piece of code will display an
error message:
Error CS0029 : Cannot implicitly convert type 'int' to 'bool'
Programming in C# /
Chapter 1/ 14 of 39
The switch Statement
(1)
The switch statement can be used in place
of
multiple if statements
Syntax:
Programming in C# /
Chapter 1/ 15 of 39
The switch Statement
(2)
Example:
It is mandatory to specify a break
statement
Programming
in C# /
Chapter 1/ 16 of 39
for each and every case block.
Iteration
Constructs
Performs
a certain set of instructions a
certain
number of times or while a specific
condition
Types of iteration constructs:
is true.
Programming in C# /
Chapter 1/ 17 of 39
The while loop
The while loop iterates through the
specified
statements
till the specified condition is
Syntax
:
true.
The break statement breaks out of the loop at
anytime.
The continue statement skips
the current
Programming
in C# /
iteration and begins with the
next
Chapter
1/ 18 of 39
iteration.
The do loop
Syntax:
Programming in C# /
Chapter 1/ 19 of 39
The for loop
Syntax :
Programming in C# /
Chapter 1/ 20 of 39
The foreach loop (1)
The foreach loop is used to iterate through a
collection or an array.
Syntax:
Programming in C# /
Chapter 1/ 21 of 39
The foreach loop (2)
Example:
Output:
Programming in C# /
Chapter 1/ 22 of 39
Constructors in C#
A constructor in C# has the same
name as
the class.
Programming in C# /
Chapter 1/ 23 of 39
Destructors in C#
A destructor in C# also has the same name
as the class preceded by a tilde (~).
Programming in C# /
Chapter 1/ 24 of 39
Fundamental Types Of C#
(1)
C# divides data types into two fundamental
categories:
- int, char and structures
- classes, interfaces, arrays and strings
Programming in C# /
Chapter 1/ 25 of 39