Programming in C# / Chapter 1/ 2 of 39Objectives Explain the need for C# Discuss flow control of a C# program Explain the fundamental data types in C# Discuss the concept of Boxi
Trang 1Chapter 1
Trang 2Programming in C# / Chapter 1/ 2 of 39
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
Compile and run a C# program
Trang 3Programming in C# / Chapter 1/ 3 of 39
Microsoft net
Represents a new platform for developing
windows & web applications
Supports more than 20 languages
Revolutionizes facilities available for Windows programming
Trang 4Programming in C# / Chapter 1/ 4 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
Trang 5Programming in C# / Chapter 1/ 5 of 39
C# Program Flow
A simple C# program:
Trang 6Programming in C# / Chapter 1/ 6 of 39
C# Constructs (1)
Variables in C# are declared as follows:
Trang 7Programming in C# / Chapter 1/ 7 of 39
Trang 8Programming in C# / Chapter 1/ 8 of 39
Trang 9Programming in C# / Chapter 1/ 9 of 39
Default Values (2)
The default values of the common data types:
Trang 10Programming in C# / Chapter 1/ 10 of 39
Trang 11Programming in C# / Chapter 1/ 11 of 39
The highlighted line acts as a placeholder where the value of the specified variable (result) will be displayed
Input / Output In C# (2)
Trang 12Programming in C# / Chapter 1/ 12 of 39
The program will accept a line from the user and echo it back as output
Input / Output In C# (3)
Trang 13Programming in C# / Chapter 1/ 13 of 39
Trang 14Programming in C# / Chapter 1/ 14 of 39
Trang 15Programming in C# / Chapter 1/ 15 of 39
The switch Statement
Trang 16Programming in C# / Chapter 1/ 16 of 39
Trang 17Programming in C# / Chapter 1/ 17 of 39
Trang 18Programming in C# / Chapter 1/ 18 of 39
The while loop
The while loop iterates through the
The continue statement skips the current
iteration and begins with the next
iteration
Trang 19Programming in C# / Chapter 1/ 19 of 39
The do loop
Syntax:
Trang 20Programming in C# / Chapter 1/ 20 of 39
Syntax :
Trang 21Programming in C# / Chapter 1/ 21 of 39
The foreach loop is used to iterate through a collection or an array
Syntax:
Trang 22Programming in C# / Chapter 1/ 22 of 39
Example:
Output:
Trang 23Programming in C# / Chapter 1/ 23 of 39
Constructors in C#
A constructor in C# has the same
name as
the class
Trang 24Programming in C# / Chapter 1/ 24 of 39
Destructors in C#
A destructor in C# also has the same name
as the class preceded by a tilde (~)
Trang 25Programming in C# / Chapter 1/ 25 of 39
Fundamental Types Of C# (1)
C# divides data types into two fundamental categories:
- int, char and structures
- classes, interfaces, arrays and strings
Trang 26Programming in C# / Chapter 1/ 26 of 39
Holds a value in the memory
Stored in a stack
Contains the address of
the object in the heap
= null means that no object has been
referencedFundamental Types Of C#
(2)
Trang 27Programming in C# / Chapter 1/ 27 of 39
Value Types
Example:
Output:
Trang 28Programming in C# / Chapter 1/ 28 of 39
Reference Types
Example:
Output:
Trang 29Programming in C# / Chapter 1/ 29 of 39
Value types vs Reference types
Trang 30Programming in C# / Chapter 1/ 30 of 39
Boxing & Unboxing [1]
Boxing is the conversion of a value type
into a
reference type
Unboxing is the conversion of a reference type into a value type
Trang 31Programming in C# / Chapter 1/ 31 of 39
Boxing & Unboxing (2)
Trang 32Programming in C# / Chapter 1/ 32 of 39
Data Types In C#
C# provides a Unified Type System
All data types in C#, are derived from just one class, the object class
Trang 33Programming in C# / Chapter 1/ 33 of 39
Trang 34Programming in C# / Chapter 1/ 34 of 39
Arrays
A group of values of similar data type
It belongs to the reference type and hence, are stored on the heap
The declaration of arrays in C#, follow the syntax given below:
DataType [ number of elements ] ArrayName ;
int [ 6 ] arr1 ;
Trang 35Programming in C# / Chapter 1/ 35 of 39
Structures
Custom data Types
Can have constructors
Cannot implement
inheritance
Can have methods
Trang 36Programming in C# / Chapter 1/ 36 of 39
Enumerators (1)
They are a set of named constants
Trang 37Programming in C# / Chapter 1/ 37 of 39
Enumerators (2)
Enumerators in C# have numbers associated
with the values
By default, the first element of enum is assigned
a value of 0 and is incremented for each
subsequent enum element
Trang 38Programming in C# / Chapter 1/ 38 of 39
Enumerators (3)
The default value can be overridden during
initialization
Trang 39Programming in C# / Chapter 1/ 39 of 39
Compiling & Running
Step 1 – Type your code in Notepad
Step 2 – Save the file with a cs extension
Step 3 – Switch to DOS prompt and type the following command: