4. Object-oriented programming
• OOP is a way of organizing the code in a program by grouping it
into objects
• Object is an individual element that includes information (data
values) and functionality
Body
Head
Eye
Nose
Mouth
Ear
Hand
Finger
Leg
Finger
5. Class
• classes describe the type of objects
• objects are usable instances of classes
• the act of creating an object is called instantiation
6. class Eye
{
public string Color { get; set; }
}
class Ear
{
public void Listen()
{
Console.WriteLine("Listening...");
}
}
class Finger
{
public string NailColor { get; set; }
}
class Hand
{
public Finger[] Fingers { get; set; } = new Finger[5];
}
class Leg
{
public Finger[] Fingers { get; set; } = new Finger[5];
}
class Mouth
{
public bool IsMouthOpened { get; set; }
public void Open()
{
Console.WriteLine("Mouth is opened.");
}
public void Close()
{
Console.WriteLine("Mouth is closed.");
}
}
class Nose
{
public void ToSniff()
{
Console.WriteLine("Sniffing...");
}
public void ToSneeze()
{
Console.WriteLine("Sneeze...");
}
}
class Head
{
public Eye LeftEye { get; set; }
public Eye RightEye { get; set; }
public Mouth Mouth { get; set; }
public Ear LeftEar { get; set; }
public Ear RightEar { get; set; }
public Nose Nose { get; set; }
}
class Body
{
public string Name { get; set; }
public int Weight { get; set; }
public int High { get; set; }
public Head Head { get; set; }
public Hand[] Hands { get; set; } = new Hand[2];
public Leg[] Legs { get; set; } = new Leg[2];
}
Classes demo:
7. Value Types
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 10
28
to 7.9 x 10
28
) / 10
0 to 28
0.0M
double 64-bit double-precision
floating point type
(+/-)5.0 x 10
-324
to (+/-)1.7 x 10
308
0.0D
float 32-bit single-precision floating
point type
-3.4 x 10
38
to + 3.4 x 10
38
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
9. Stack and Heap (Demo)
• 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.
Stack Heap
Value types
Pointers to the reference types
Reference types
10. Boxing and unboxing
System.Object is a parent type for all other types
Boxing is the process of converting a value type to the type object.
The reverse process called unboxing.
int i = 123;
// The following line boxes i.
object o = i;
o = 123;
i = (int)o; // unboxing
11. Mutable and Immutable types
• 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
12. Nullable type
int? value = null;
Console.WriteLine(value.HasValue);
//
// Assign the nullable integer to a constant integer.
// ... The HasValue property is now true.
// ... You can access the Value property as well.
//
value = 1;
Console.WriteLine(value.HasValue);
Console.WriteLine(value.Value);
Console.WriteLine(value);