Lesson 1
During this lesson we’ll review what Object-oriented programming is. Also we’ll learn C# types and create a first program.
Agenda
- Object oriented programming
- Stack and Heap
- Value types in C#
- Boxing and unboxing
- Mutable and Immutable objects
- Nullable type
- Presentation
- References
Object oriented programming
OOP is a way of organizing the source code in a program by grouping it into objects. Object is an individual element that includes information (data values) and functionality.
Picture below demonstrates “objects” in a real life.
And here is an example how it can be coded in programming language:
In C# even a simplest program is a class. Function Main is an entry point in application. It means, that function Main will be called first when application starts.
Stack and Heap
- Value Type - stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value.
- Reference Types - are used by a reference which holds a reference (address) to the object but not the object itself. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn’t copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value.
Value types in C#
Type | Represents | Range | Default Value |
---|---|---|---|
bool | Boolean value | True or False | False |
byte | 8-bit unsigned integer | 0 to 255 | 0 |
char | 16-bit Unicode character | U +0000 to U +ffff | '\0' |
decimal | 128-bit precise decimal values with 28-29 significant digits | (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 | 0.0M |
double | 64-bit double-precision floating point type | (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 | 0.0D |
float | 32-bit single-precision floating point type | -3.4 x 1038 to + 3.4 x 1038 | 0.0F |
int | 32-bit signed integer type | -2,147,483,648 to 2,147,483,647 | 0 |
long | 64-bit signed integer type | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
sbyte | 8-bit signed integer type | -128 to 127 | 0 |
short | 16-bit signed integer type | -32,768 to 32,767 | 0 |
uint | 32-bit unsigned integer type | 0 to 4,294,967,295 | 0 |
ulong | 64-bit unsigned integer type | 0 to 18,446,744,073,709,551,615 | 0 |
ushort | 16-bit unsigned integer type | 0 to 65,535 | 0 |
Boxing and unboxing
System.Object is a parent type for all other types. I.e. all types are inherited from Object type.
Boxing is the process of converting a value type to the type object. The reverse process called unboxing.
Mutable and Immutable objects
- Mutable type – multiple values can be assigned to mutable object many times and its state can be altered
- Immutable type – immutable classes are read-only. Once declared their value cannot be changed
String is a good example of immutable type:
StringBuilder is mutable type:
Nullable type
C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.