More About Classes and Objects In C#

In our previous post, we looked at classes and objects in C#. We will dig deep in this post to learn more about classes and objects and how it works behind the scenes. Let's get started.

cheezycode_more_class_and_objects_with_example



Class is a blueprint or template, using this template we create objects. Let's take more examples to easily digest this easy concept. We all love cookies but how are they made. Take dough with some chocolate chips inside and with the help of Cookie Cutter, we get such delicious cookies in different designs.


cheezycode-cookie cutter-class-blueprint
Cookie Cutter (Class)
cheezycode-cookie-actual-objects
Cookies (Actual Object)

Now you must have understood the concept of classes and objects. Class is used to create objects whereas objects are the real thing. You create objects in your program and they interact with each other to complete the work.


Behind the scenes


To create objects in C#, we use new keyword. For e.g Car beetle = new Car(); This will create a new car object named beetle using Car class definition. new keyword allocate the memory for the object dynamically and returns the reference of the object. Let's see step by step -

1. In Car beetle = new Car(); this code statement, new Car() will be executed first. It goes into the memory to allocates the required space in bytes. These bytes are calculated based on the properties or attributes in your object.

2. If it can successfully allocate the space, it will create an object and returns a reference. You can consider this reference as a handle to access the object. If somehow, memory could not be allocated, you will get OutOfMemoryException. This exception is very rare although.

3.  Now to interact with this object and manipulate this object , we need to store this reference by which we will make any changes. Car beetle = new Car(); now beetle will hold the reference. In simple words, new Car() returns reference and beetle stores it.

4. After that you can access the properties of this beetle object or you can call methods of this class by using this beetle reference. To assign values to its properties, use dot operator -

beetle.modelYear = 2017;

To access the values stored in the beetle object like it's name again use dot operator -

string name = beetle.carName;

NOTE: There is a special method Constructor which is called once the object is created and default values of each attribute is assigned. This method is called before passing the reference. Main purpose of the method is to initialize the values of properties. More info in our next post.


What if I want to create 2 objects?


Same process is followed to create objects. Both will have their own reference to access the object in memory. Although they are created using the same template Car but they are totally different from each other. Refer this image below to understand the concept -





You can refer the slides for this post here as well -



Let us know if you have any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)



Comments

Popular posts from this blog

Just Maths - Views, View Groups and Layouts

Create Android Apps - Getting Started