NET-001.Module 2
NET-001.Module 2
part 2: Foundations of
Programming
www.luxoft-training.com
2
www.luxoft-training.com
3
Memory
Variables. Initialization
Nested and user-defined data types
Implicit typing
Visibility Scope
www.luxoft-training.com
4
Variables
www.luxoft-training.com
5
Initializing a variable
www.luxoft-training.com
6
There are the following visibility
scopes:
Code block context
Method context
Class context
Assembly context
www.luxoft-training.com
7
Sequential execution blocks
Are enclosed In a child block you In the same
between curly cannot declare a level block –
braces variable with the same you can do it
name as in the parent {
{
block int i;
// code
{ ...
}
int i; }
... ...
{ {
int i; int i;
... ...
} }
}
www.luxoft-training.com
8
Data Types
www.luxoft-training.com
9
Types of Variables
There are two kinds of types in C#: reference types and value types
www.luxoft-training.com
10
Value Types
Value types:
Usually placed in the stack
A variable contains data
Each variable changes its data only
Assigning = copying
www.luxoft-training.com
11
Reference Types
Reference types:
Memory is allocated from the bulk
Variables store a reference to data (objects)
Two reference variables can refer to the same object
Changing properties of one object results in changing the other
www.luxoft-training.com
12
42 • Hello
www.luxoft-training.com
13
coordinate c1;
c1 = new coordinate();
c1.x = 6.12;
• 6.12 4.2
c1.y = 4.2;
Release
• 1.0 2.0
Different
• 1.0 2.0
www.luxoft-training.com
15
c1 •
2.3 7.6
c2 •
coordinate c1= new coordinate( );
coordinate c2;
c1.x = 2.3; c1.y = 7.6;
c2 = c1;
Console.WriteLine(c1.x + " , " + c1.y);
Console.WriteLine(c2.x + " , " + c2.y);
www.luxoft-training.com
16
Incorrect references
While compiling
The compiler warns about an error when it encounters the use of non-
initialized references.
www.luxoft-training.com
17
www.luxoft-training.com
18
Data size
www.luxoft-training.com
19
www.luxoft-training.com
20
Primitive types
Are directly supported by the compiler
Have simplified syntax
www.luxoft-training.com
22
checked { OverflowException
int number = int.MaxValue;
Console.WriteLine(++number); Exception object is thrown.
} WriteLine is not executed.
Enumerations
Enum is a user-defined data type.
A variable of this type can take values strictly from
your list of enumerations.
www.luxoft-training.com
24
Enumerations
Usually the best practice is to define enumeration directly in the
namespace so that all classes in the namespace can easily access
it.
www.luxoft-training.com
25
Operators and Statements
www.luxoft-training.com
Relational operators and logical operators 26
== Equality
!= Inequality
> Greater than
<
Less than
>=
Greater than or equal
<=
Less than or equal
&
Integer logical bitwise AND operator
|
Integer logical bitwise OR operator
^
Integer logical bitwise XOR operator
&&
Conditional logical AND operator
||
Conditional logical OR operator
!
Logical negation operator
www.luxoft-training.com
27
Directive ‘using’
The directive using is used for the following three purposes :
To allow using types in the namespace so that you don’t need to
qualify using the type in this namespace
www.luxoft-training.com
28
Directive ‘using’
To create an alias for a namespace or type
www.luxoft-training.com
29
Directive ‘using’
www.luxoft-training.com
30
System.Console Class
Access to standard input, output and error flows.
Valid for console applications only:
Standard input – keyboard
www.luxoft-training.com
31
www.luxoft-training.com
32
www.luxoft-training.com
33
Operators
Operator priorities and the order of execution
Except assignment operators, all other operators are applied from left to right
Assignment operators and the operator ?: are applied from right to left
Use brackets
Some operators can be redefined
www.luxoft-training.com
34
Execution priority
Common Operators Example
• Equality == !=
• Comparison < > <= >= is
• Conditionals && || ?:
• Adding 1 ++
• Subtracting 1 --
• Arithmetic + - * / %
• Assignment operators = *= /= %= += -=
<<= >>= &= ^=
|=
www.luxoft-training.com
35
Assignment operators
+= x = x + 1;
-= x = x - 1;
*= x = x*1;
/= x = x/1;
%= x = x%1;
|= x = x | 1;
^= x = x^1;
www.luxoft-training.com
36
Conditionals. Operator if
If the expression in brackets is true, execute the code block
void WriteLog(int severity,String msg)
{
String result="";
if (severity < 3)
{
result = "Error : ";
}
www.luxoft-training.com
38
Loops
Types of loops
Early loop exit
Practice
www.luxoft-training.com
39
For loop
Using the for loop, you can once and again execute an operator or a
block of operators until a certain expression takes the value false.
for (initializer; condition; iterator)
body
www.luxoft-training.com
40
While loop
The while operator executes an operator or a block of operators until
a certain expression takes the value false.
www.luxoft-training.com
41
Do while loop
The construction do while guarantees that the loop body is executed
at least once, irrespective of any condition
www.luxoft-training.com
42
www.luxoft-training.com
43
Practice
Arrange for continuous input of numbers from the keyboard until the
user enters 0. Once zero is entered, display on the screen the number
of positive numbers, that were entered, and negative numbers, and
the arithmetic average.
To convert the entered symbols into numbers, use the function Int32.TryParse;
www.luxoft-training.com
44
Functions
Definition
Function signature
Function overloading
Passing arguments to the function
Perfect function
www.luxoft-training.com
45
Definition
Function is a fragment of program code (subprogram) which you can
address from another location in the program.
Example:
www.luxoft-training.com
46
Functions in С#
www.luxoft-training.com
47
Function signature
Signatures must be unique within a class
www.luxoft-training.com
50
Perfect function
www.luxoft-training.com
51
www.luxoft-training.com
52
www.luxoft-training.com
53
www.luxoft-training.com
54
c2
www.luxoft-training.com
55
Calling
www.luxoft-training.com
56
Demonstration of passing
various parameters
DEMO
www.luxoft-training.com
57
Practice
The keyboard is used to input 4 values:
Integers :
Height
Day of week
Line :
Gender
Name
www.luxoft-training.com
58
Practice (continued)
There are 3 attractions in the park:
Batman (open Mon, Wen, Fri; only for boys over 150 cm tall)
Swan (open Tue, Wen, Thu; for girls over 120 and below 140 cm
tall, and boys below 140 cm tall)
Pony (for all, open daily, except Sun)
www.luxoft-training.com
59
www.luxoft-training.com
60
DRY:
“Every piece of knowledge must have a single, unambiguous,
authoritative representation within a system.” Andrew Hunt, David
Thomas "The Pragmatic Programmer"
Don’t duplicate your code — take it out to functions
www.luxoft-training.com
61
Arrays
Definition
Practice
www.luxoft-training.com
62
Arrays
An array is an ordered collection of elements of the same data type.
It is accessed through the array name together with the offset of the required
element from the start of the array.
365 24 12 60 30
www.luxoft-training.com
63
Practice
Conditions of the previous task (see Attraction Evolution)
Task:
Change your code so that you can get a list (array) of all attractions which a
child can enjoy depending on the child’s characteristics (the access rules
remain unchanged).
In the previous version, the first suitable attraction was displayed, and now
you should display a list of suitable attractions.
www.luxoft-training.com
64
Arrays
In С# you can create multidimensional and jagged arrays. You should
study them if you go deeper to algorithms.
www.luxoft-training.com
65
Summary
Memory. Variables. Visibility Scope
Data Types
Arithmetical and logical operations
Conditionals
loops
Functions
Arrays
www.luxoft-training.com
66
Questions
www.luxoft-training.com