Unit - 1
Unit - 1
The C#
Language
Road Map
• Arrays • Indexers
• Pointers • Enumerations
Introductio
n
Introduction
• Simple:
• Modern:
• Object-Oriented:
• Type Safety:
• Value Types
• Pointer Types
• Reference Types
Type
s
Types: Value
Types
• Value Type variables directly store the data in
memory.
• Simple Types:
string System.String
object System.Object
Types: Reference Types
• Reference Types:
• string type:
• Reference Types:
• object type:
• Float Constants
• Character Constants
• String Constants
Constants
• Integer Constants:
• It’s either a decimal or a hexadecimal value.
• Float Constants:
• It’s a float or a double value.
• Character Constants:
• Character Constants are enclosed in single
quotes.
• A plain character
• An escape sequence
Constants
• String Constants:
• String is a collection of characters that is enclosed in double
quotes.
• Categories of Operators in
C#:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Increment/Decrement
Operators
• Conditional Operator
• Miscellaneous Operators
Operators: Arithmetic Operators
Operator Description
+ Adds two operands
- Subtracts second operand from the first
* Multiplies both operands
/ Divides numerator by de-numerator
% Modulus Operator and remainder of after an integer division
Operators: Relational Operators
< Checks if the value of left operand is less than the value of right
operand.
Checks if the value of left operand is greater than or equal to
>=
the value of right operand.
Operator Description
Called Logical AND operator. If both the operands are non
&&
zero then condition becomes true.
Called Logical OR Operator. If any of the two operands is non
||
zero then
condition becomes true.
Called Logical NOT Operator. Use to reverses the logical
! state of its operand. If a condition is true then Logical NOT
operator will make false.
Operators: Bitwise Operators
Operator Description
Operator Description
if…else if…
else:
Control Structures: Decision Making
While loop:
While loop repeats a block predefined or unknown number of
times.
It is an Entry Controlled loop because the condition is checked
before the
body of loop is executed.
Control Structures: Looping
Do…while loop:
Do…while loop repeats a block predefined or unknown number
of times.
It is an Exit Controlled loop because the condition is checked
after the
body of loop has been executed at least once.
Control Structures: Looping
Foreach loop:
Foreach loop is used for iterating through the elements of an
array or a
collection.
It copies individual values from the collection into a variable
as the loop progresses.
Control Structures: Looping
Method Description
Default Constructor:
It does not accept any parameters. If a constructor is not
specified, C#
automatically adds a default constructor.
Classes & Objects: Constructor
Parameterized Constructor:
It accepts 1 or more parameters which will be used to
initialize the data
member of the class when the object is created.
Classes & Objects: Constructor
Static Constructor:
Static Constructor is created using the static
keyword.
No access modifier is specified for a static
constructor.
It cannot accept any parameters.
It’s called automatically when the class is loaded into
memory.
Classes & Objects: Constructor
Private Constructor:
It’s a default or a parameterized constructor which has a
private access
modifier.
An object of a class cannot be created which is having
a Private Constructor.
Private Constructors are generally created in a class
which only has static
members to prevent object creation.
Classes & Objects: Destructor
parent.
Inheritan
ce
Accessing base class
members:
Inheritance
Synta
x:
ArrayList:
An ordered list of items (think of it as a dynamic array).
Each item can be accessed/updated individually using its 0-
based index.
Supports item addition, insertion, updating and removal.
Resizes automatically as items are added/removed.
Click here for an example
Collections: Non-generic
Hashtable:
A collection of key-value pairs.
Supports custom indexing. May not maintain ordering.
Each item can be accessed/updated individually using its
custom index.
Click here for an example
Collections: Non-generic
SortedList:
A list of key-value pairs, supports 0-based indexing and custom
indexing.
Basically, a combination of ArrayList and Hashtable.
Items are sorted by Keys.
Click here for an example
Collections: Non-generic
Stack:
Stores and retrieves data using Last-In-First-Out (LIFO)
approach.
Click here for example
Supports Push, Pop and Peek operations:
Push adds an item at the Top of the Stack (TOS)
Pop removes an item from the TOS and returns it.
Peek returns an item from the TOS without removing
it.
Click here for an example
Collections: Non-generic
Queue:
Stores and retrieves data using First-In-First-Out (FIFO)
approach.
Supports Enqueue, Dequeue and Peek operations:
Enqueue appends an item into the queue.
Dequeue removes an item from the end and returns
it.
Peek returns an item from the end without removing
it.
Click here for an example
Collections: Generic
Add the using the + operator and assign the result into a 3rd
delegate object.
Events are some occurrence of a user action, such as, key press,
button click.
C# provides a mechanism to call a method when an event occurs.
Before using events, their delegates must be created.
Basically, events are a collections of delegates which are called
when user performs a specific operation.
See this example
Methods can be added into an event using += operator and,
similarly, removed
using -= operator.