Introduction to C#
- slide 14 : 43
Commands and Control Structures
Almost all control structures in C can be used the same way in C#
Similarities
Expression statements, such as
a = a + 5
;
Blocks, such as
{a = 5; b = a;}
if
,
if-else
,
switch
,
for
,
while
,
do-while
,
return
,
break
,
continue
, and
goto
in C# are all similar to C
Differences
The C#
foreach
loop provides for easy traversal of all elements in a collection
try-catch-finally
and
throw
in C# are related to exception handling
Some more specialized statements have been added:
checked
,
unchecked
,
using
,
lock
and
yield
.
Demonstrations of definite assignment.
Demonstrations of if.
Demonstrations of switch.
Demonstrations of foreach.
Demonstrations of try catch.