Você está na página 1de 47

Basics of .

NET Development and C#


Language
Aleksey Strakh
astrakh@luxoft.com

Curriculum

Course duration is 40 hours (5 days)


11 modules in 3 blocks
3 main blocks
Block 1. Platform overview

Module 1. .NET platform overview


Module 2. C# language overview

Block 2. Type design

Module 3. Basics of type design


Module 4. Classes, objects, and OOP principles
Module 5. Type members
Module 6. Generalizations

Block 3. Main types

Module 7. Symbols, strings, and text processing


Module 8. Enumerations and bit flags
Module 9. Arrays
Module 10. User attributes
Module 11. Nullable types

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

Good manner one file, one class

The Main method


Applications entry point
C# is case-sensitive language
The Main syntax

There could be only one Main method


Exiting from Main is closing the application

The using keyword and


System namespace
.NET Framework classes are organized in
namespaces
System contains declarations of main types, e.g.
System.Int32, System.Console
Full class name is <namespace>,<class name>:

The using keyword:

Demo

Using Visual Studio to Create a C#


Program

Basic I/O functions

The Console Class


Write and WriteLine Methods
Read and ReadLine Methods

The System.Console class


Access to standard input, standard output, and
standard error streams.
Relevant to console applications only:
Standard input keyboard
Standard output screen
Standard error screen
Standard streams can be redirected, for example,
to file

The Write and WriteLine methods


Console.Write and Console.WriteLine display
information on the screen
WriteLine adds newline characters (\r\n)
Both methods has overloads
Format line can be used to
Format text
Format numbers

The Read and ReadLine methods


Console.Read and Console.ReadLine read user
input
Read read a character
ReadLine read a line

General coding guidelines


Comments
Using XML Documentation format
Demo: Generating and viewing XML
documentation
Exception Handling

Comments
It is good practice to use comments
Allow developer to understand the meaning of
the code faster
One-line comment:

Multi-line comment:

Using XML Documentation format

Demo

Generating and viewing XML


documentation

Exception handling

Compiling, running, and


debugging

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

From command line


Write the applications name
From Visual Studio
Start Without Debugging in
Debug menu

Demo

Compiling and Running C# Program

Debugging

The Visual Studio Debugger


Exceptions
Setting breakpoints and
program-stop conditions
Code navigation
Reading/changing variables
values during debugging

Demo

Using Visual Studio Debugger

Control structures

Sequential execution blocks


(Statement Blocks)
Branching
Loops
Unconditional jumps (jump
statements)

Sequential execution blocks

Enclosed in curly
brackets
{{
////code
code
}}

Its not allowed declare a


variable in a child block
with the same name as in
a parent block
{{
int
inti;i;
......
{{
int
inti;i;
......
}}
}}

But its allowed


in the blocks of
the same level
{{
int
inti;i;
......
}}
......
{{
int
inti;i;
......
}}

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

In contrast to C++/C int is not implicitly


converted to bool
int
int x;
x;
...
...
if
//
if (x)
(x) ...
...
// Must
Must be
be if
if (x
(x !=
!= 0)
0) in
in C#
C#
if
if (x
(x == 0)
0) ...
... //
// Must
Must be
be if
if (x
(x ==
== 0)
0) in
in C#
C#

Cascading if statements

The switch statement

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

The while Statement


The do Statement
The for Statement
The foreach Statement
Task: what will be the result?

The while statement


Nested structures executed depending on
the results of condition check
Evaluation of Boolean expression occurs in
the beginning of the loop
Nested structures continue to execute
while expression is True
int
int ii == 0;
0;
while
while (i
(i << 10)
10) {{
Console.WriteLine(i);
Console.WriteLine(i);
i++;
i++;
}}

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

The for statement


Condition is checked in the beginning, while change
of the counter in the end
for
for (int
(int ii == 0;
0; ii << 10;
10; i++)
i++) {{
Console.WriteLine(i);
Console.WriteLine(i);
}}

0 1 2 3 4 5 6 7 8 9

Scope of counter is within a block


for
for (int
(int ii == 0;
0; ii << 10;
10; i++)
i++)
Console.WriteLine(i);
Console.WriteLine(i);
Console.WriteLine(i);
Console.WriteLine(i); //
// Error:
Error: ii is
is no
no longer
longer in
in scope
scope

There can be several counter variables


for
for (int
(int ii == 0,
0, jj == 0;
0; ...
... ;; i++,
i++, j++)
j++)

The foreach keyword


Block execution for all elements of
enumerated data types
ArrayList
ArrayList numbers
numbers ==
for
for (int
(int ii == 0;
0; ii <<
numbers.Add(i);
numbers.Add(i);
}}

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

Task: what will be the result?


for
for (int
(int ii == 0,
0, ii << 10,
10, i++)
i++)
Console.WriteLine(i);
Console.WriteLine(i);

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:;

break and continue


break exit from the loop prematurely
continue start the next iteration
immediately
int
int ii == 0;
0;
while
while (true)
(true) {{
Console.WriteLine(i);
Console.WriteLine(i);
i++;
i++;
if
if (i
(i << 10)
10)
continue;
continue;
else
else
break;
break;
}}

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

< > <= >= is

Conditional

&& || ?:

Increment 1

++

Decrement 1

--

Arithmetic

+ - * / %

Assignment

= *= /= %= += -=
<<= >>= &= ^=
|=

Check for Arithmetic Overflow


By default, C# does not check numeric
types for overflow
The checked block includes check for
overflow
checked
checked {{
int
int number
number == int.MaxValue;
int.MaxValue;
Console.WriteLine(++number);
Console.WriteLine(++number);
}}
unchecked
unchecked {{
int
int number
number == int.MaxValue;
int.MaxValue;
Console.WriteLine(++number);
Console.WriteLine(++number);
}}

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

Creating Simple C# Application

Questions?

Você também pode gostar