Learn how objects are created from classes in C# behind the scenes. What happens in the memory and learn how to access properties of an object and assign values to it. Visualize the process by this simple steps.
Visit http://www.cheezycode.com/2016/08/more-about-classes-and-objects-in-csharp.html
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
Classes and Objects In C# With Example
1. Car beetle = new Car();
Object Creation - Behind the scenes
{CHEEZYCODE}
2. Car beetle = new Car();
class Car
{
//Properties
public int modelYear;
public string carName;
public string carType;
public float price;
//Methods
public void DriveCar()
{
Console.WriteLine("Car is driving");
}
public void ApplyBrake()
{
Console.WriteLine("Car is applying brakes");
}
}
{CHEEZYCODE}
3. Car beetle = new Car(); 1
class Car
{
//Properties
public int modelYear;
public string carName;
public string carType;
public float price;
//Methods
public void DriveCar()
{
Console.WriteLine("Car is driving");
}
public void ApplyBrake()
{
Console.WriteLine("Car is applying brakes");
}
} {CHEEZYCODE}
4. Car beetle = new Car(); 1 When we say new Car(), new
object in the memory is created.
class Car
{
//Properties
public int modelYear;
public string carName;
public string carType;
public float price;
//Methods
public void DriveCar()
{
Console.WriteLine("Car is driving");
}
public void ApplyBrake()
{
Console.WriteLine("Car is applying brakes");
}
}
{CHEEZYCODE}
5. Car beetle = new Car(); 1 When we say new Car(), new
object in the memory is created.
Memory Locations
{CHEEZYCODE}
6. Car beetle = new Car(); 1 When we say new Car(), new
object in the memory is created.
Object will occupy certain
bytes in memory
Memory Locations
{CHEEZYCODE}
7. Car beetle = new Car(); 1 When we say new Car(), new
object in the memory is created.
Memory Locations
{CHEEZYCODE}
8. Car beetle = new Car(); 1 When we say new Car(), new
object in the memory is created.
Memory Locations
{CHEEZYCODE}
9. Car beetle = new Car(); 1 When we say new Car(), new
object in the memory is created.
This is new object of
Car in memory
Memory Locations
{CHEEZYCODE}
10. Car beetle = new Car(); 2
This is new object of
Car in memory
Memory Locations
Object has been created, now
location of this object is passed
as a reference
{CHEEZYCODE}
11. Car beetle = new Car(); 3
This is new object of
Car in memory
Memory Locations
Location of this object is stored in
beetle to access the car object
{CHEEZYCODE}
12. Car beetle = new Car(); 4
beetle
Memory Locations
Now you can access newly created
object in memory with the help of
beetle reference
{CHEEZYCODE}
13. Car beetle = new Car(); 5
beetle
What does this object has?
{CHEEZYCODE}
14. Car beetle = new Car(); 5
beetle
What does this object has?
class Car
{
//Properties
public int modelYear;
public string carName;
public string carType;
public float price;
//Methods
public void DriveCar()
{
Console.WriteLine("Car is driving");
}
public void ApplyBrake()
{
Console.WriteLine("Car is applying brakes");
}
}
Remember this ?
{CHEEZYCODE}
15. Car beetle = new Car(); 6
beetle
Object stores properties values - it
could be another object or built in
types like int or float
class Car
{
//Properties
public int modelYear;
public string carName;
public string carType;
public float price;
//Methods
public void DriveCar()
{
Console.WriteLine("Car is driving");
}
public void ApplyBrake()
{
Console.WriteLine("Car is applying brakes");
}
} {CHEEZYCODE}
16. 7beetle beetle object in memory has
these properties
class Car
{
//Properties
public int modelYear;
public string carName;
public string carType;
public float price;
//Methods
public void DriveCar()
{
Console.WriteLine("Car is driving");
}
public void ApplyBrake()
{
Console.WriteLine("Car is applying brakes");
}
}
modelYear (Integer)
carName (String)
carType(String)
price(float)
{CHEEZYCODE}
17. 8 What if I want to create 2 objects?
{CHEEZYCODE}
18. 8 What if I want to create 2 objects?
Car beetle = new Car();
Car mustang = new Car();
{CHEEZYCODE}
19. 8 What if I want to create 2 objects?
Car beetle = new Car();
Car mustang = new Car();
Same process will be followed
{CHEEZYCODE}
20. 8 What if I want to create 2 objects?
modelYear (Integer)
carName (String)
carType(String)
price(float)
modelYear (Integer)
carName (String)
carType(String)
price(float)
beetle mustang
{CHEEZYCODE}
21. 8
beetle
What if I want to create 2 objects?
modelYear (Integer)
carName (String)
carType(String)
price(float)
modelYear (Integer)
carName (String)
carType(String)
price(float)
mustang
BOTH
OBJECTS
ARE
DIFFERENT
{CHEEZYCODE}
22. 9
Assign values to these properties
using dot(.) operator
beetle
modelYear (Integer)
carName (String)
carType(String)
price(float)
Assigning values to
these properties.
These are similar to
variables but they are
inside beetle and part of
beetle object.
{CHEEZYCODE}
24. 10
Access these properties using dot
operator
beetle
modelYear (Integer)
carName (String)
carType(String)
price(float)
You access the
properties of beetle
using dot operator
{CHEEZYCODE}
25. 10
Assign values to these properties
using dot(.) operator
beetle
modelYear (Integer)
carName (String)
carType(String)
price(float)
Console.WriteLine(beetle.modelYear);
and similarly you access other
properties of object
{CHEEZYCODE}