Escolar Documentos
Profissional Documentos
Cultura Documentos
Curriculum
Module 2. C# Overview
Plan:
C# program structure
Main I/O operations
General coding guidelines
Compiling, debugging, and execution
Control structures
Operators
C# program structure
Hello, World
The Class
The Main method
The using keyword and System namespace
Demo: Using Visual Studio to Create
a C# Program
Hello, World
The Class
C# application is a set of types (classes and
structures)
Class is a set of data and associated methods
Syntax
Demo
Comments
It is good practice to use comments
Allow developer to understand the meaning of
the code faster
One-line comment:
Multi-line comment:
Demo
Exception handling
Compiling application
Running application
Demo: Compiling and Running
C# Program
Debugging
Demo: Using Visual Studio
debugger
Compiling application
Compilation model
Main compiler options
Compiling from command line
csc.exe
Building using Visual Studio
Locating Errors
Running application
Demo
Debugging
Demo
Control structures
Enclosed in curly
brackets
{{
////code
code
}}
Branch statements
The if Statement
Cascading if Statements
The switch Statement
Task: find an error
The If statement
Syntax:
if
if (( Boolean-expression
Boolean-expression ))
first-embedded-statement
first-embedded-statement
else
else
second-embedded-statement
second-embedded-statement
Cascading if statements
Find an error
if
if number
number %% 22 ==
== 00
...
...
11
if
if (percent
(percent << 0)
0) ||
|| (percent
(percent >> 100)
100) ...
...
22
if
if (minute
(minute ==
== 60);
60);
minute
minute == 0;
0;
33
switch
switch (trumps)
(trumps) {{
case
case Suit.Clubs,
Suit.Clubs, Suit.Spades
Suit.Spades ::
color
color == "Black";
"Black";
case
case Suit.Hearts,
Suit.Hearts, Suit.Diamonds
Suit.Diamonds ::
color
color == "Red";
"Red";
default
default ::
...
...
}}
44
Loops
0 1 2 3 4 5 6 7 8 9
The do statement
Nested structures executed depending on the
results of condition check
Evaluation of Boolean expression occurs in the
end of the loop
Nested structures continue to execute while
expression is True
int
int ii == 0;
0;
do
do {{
Console.WriteLine(i);
Console.WriteLine(i);
i++;
i++;
}} while
while (i
(i << 10);
10);
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
new
new
10;
10;
ArrayList(
ArrayList( );
);
i++
i++ )) {{
foreach
foreach (int
(int number
number in
in numbers)
numbers) {{
Console.WriteLine(number);
Console.WriteLine(number);
}}
0 1 2 3 4 5 6 7 8 9
11
int
int ii == 0;
0;
while
while (i
(i << 10)
10)
Console.WriteLine(i);
Console.WriteLine(i);
22
for
for (int
(int ii == 0;
0; ii >=
>= 10;
10; i++)
i++)
Console.WriteLine(i);
Console.WriteLine(i);
33
do
do
...
...
string
string line
line == Console.ReadLine(
Console.ReadLine( );
);
guess
guess == int.Parse(line);
int.Parse(line);
while
while (guess
(guess !=
!= answer);
answer);
44
Unconditional jumps
(jump statements)
goto
break and continue
goto
Go to the named label
If used excessively, results in unreadable
code
if
if (number
(number %% 22 ==
== 0)
0) goto
goto Even;
Even;
Console.WriteLine("odd");
Console.WriteLine("odd");
goto
goto End;
End;
Even:
Even:
Console.WriteLine("even");
Console.WriteLine("even");
End:;
End:;
Operators
Operators priority and execution order
Operators apply from left to right, except for
assignment operators
Assignment operators and ? operator apply
from right to left
Use brackets
Some operators may be redefined
Execution order
Common
Common Operators
Operators
Example
Equality
== !=
Comparison
Conditional
&& || ?:
Increment 1
++
Decrement 1
--
Arithmetic
+ - * / %
Assignment
= *= /= %= += -=
<<= >>= &= ^=
|=
OverflowException
OverflowException
Exception object is thrown.
WriteLine is not executed.
MaxValue + 1 is negative?
-2147483648
Calculate
17!
Summary
C# program structure
Main I/O operations
General coding guidelines
Compiling, debugging, and
execution
Control structures
Operators
Practice
Questions?