You are on page 1of 10

Arrays

Initializing Arrays at Declaration


C# provides simple and straightforward ways to initialize arrays at declaration time by enclosing the initial values in curly
braces ({}). The following examples show different ways to initialize different kinds of arrays.
Single
Dimensional

DEMO

Multi
Dimensional

Jagged
Arrays
ReDim Preserve [VB.Net]
ReDim: Reallocate storage space for an array variable.
Preserve: Preserve the data in the existing array when you change the size of only the last dimension.

DEMO
Iteration Statements
do
Reading Arrays using foreach
for
C# provides simple and straightforward ways to initialize arrays at declaration time by enclosing the initial values in curly
foreach, in
braces ({}). The following examples show different ways to initialize different kinds of arrays.
while
Single
Dimensional

Multi
Dimensional

However, with multidimensional arrays, using a


nested for loop gives you more control over
the array elements.

Jagged
Try yourself!!
DEMO
Collections Class
do
Using foreach with collection class
for
foreach, in
 The foreach statement is a convenient way to enumerate the elements of a collection, provided that the
collection class has implemented the System.Collections.IEnumerator and System.Collections.IEnumerable
while
interfaces.
The below example (class diagram) – shows the implementation of a string tokenizer.

enumeration is referred
to listing the elements of
a collection.
Collections Class
do
Using foreach with collection class
for
foreach, in
 The below example (class diagram) – shows the implementation of a string tokenizer.
while

Using string array DEMO

Changing Object to String


will make this type-safe.
Iteration Statements
do
while (C#.Net) While (VB.Net)
for
foreach, in
The while statement executes a statement or a block of statements until a specified expression evaluates to
false.
while
while (expression)  Because the test of expression takes place before each execution of the loop, a while
{
statement(s);
loop executes zero or more times.
} A while loop can be terminated when a break, goto, return, or throw statement
transfers control outside the loop. To pass control to the next iteration without exiting
the loop, use the continue statement.
C# VB.Net

DEM
O
Jump Statements
break
C#.Net VB.Net
continue
goto
Branching is performed using jump statements, which cause an immediate transfer of the program control.
return break Terminates the closest enclosing loop
DEMO
throw continue Passes control to the next iteration of the enclosing iteration statement in which it appears.

goto Transfers the program control directly to a labeled statement.

return Terminates execution of the method in which it appears and returns control to the calling method.

throw Used to signal the occurrence of an anomalous situation (exception) during the program execution.
Exception Overview
System.Exception Overview
C# provides built-in support for handling anomalous situations, known as exceptions, which may occur
during the execution of your program.
System.Exception class represents errors that occur during application execution.

For complete list of exceptions refer:


http://msdn.microsoft.com/en-us/library/system.systemexception.aspx
http://mikevallotton.wordpress.com/2009/07/08/net-exceptions-all-of-them/
Exception Overview
System.Exception Overview
 System.Exception class represents errors that occur during application execution.

Some of the important properties of System.Exception


InnerException Gets the Exception instance that caused the current exception.
Message Gets a message that describes the current exception.
StackTrace Gets a string representation of the call stack.

Some of the important methods of System.Exception


GetBaseException Returns the Exception that is the root cause of the subsequent exceptions.
GetType Gets the runtime type of the current instance.
ToString Creates and returns a string representation of the current exception.
Exception Handling Statements
try-catch
try-catch-throw (C#.Net) Try-Catch-Throw (VB.Net)
throw
try-finally
C# provides built-in support for handling anomalous situations, known as exceptions, which may occur
during the execution of your program.
try-catch-
finally The try, throw, catch, and finally keywords implement exception handling.

C# VB.Net

DEMO

You might also like