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:
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 Nose
{
public void ToSniff()
{
Console.WriteLine("Sniffing...");
}
public void ToSneeze()
{
Console.WriteLine("Sneeze...");
}
}
class Mouth
{
public bool IsMouthOpened { get; set; }
public void Open()
{
Console.WriteLine("Mouth is opened.");
}
public void Close()
{
Console.WriteLine("Mouth is closed.");
}
}
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];
}
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.
using System;
namespace Lesson1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
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.
int i = 123;
// The following line boxes i.
object o = i;
o = 123;
// unboxing
i = (int)o;
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:
string city = "Vinnytsia";
city.ToUpper();
Console.WriteLine(city); //nothing changed
city = city.ToUpper();
// new value assigned
Console.WriteLine(city);
StringBuilder is mutable type:
StringBuilder builder = new StringBuilder();
builder.Append("Vinnytsia ");
builder.Append("is a good city!");
//Vinnytsia is a good city!
Console.WriteLine(builder);
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.
int? value = null;
Console.WriteLine(value.HasValue);//False
//
// 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);//True
Console.WriteLine(value.Value);//1
Console.WriteLine(value);//1