Introduction to classes and objects in C# vs Python

We have used various data types so far, such as, integers, floats, doubles, bools, strings and even tuples. In this section, we will differentiate between primitive and object types and look into concepts such as stack, heap, user defined types, classes and the concepts of object oriented programming that make classes useful.

Stack vs heap:

Stack is a name given to a last-in-first-out (LIFO) memory allocation. This means that the memory that is allocated last, must be freed first. For example, suppose the code within a function, say  functionONE(), is being executed. The required memory is allocated on the computer’s RAM for the function’s local data, i.e. the variables declared inside the function. This data is then said to be on the top of the Stack. Lets assume, during its execution,  functionONE() calls another function called  functionTWO(). The local data of functionTWO() now takes the top of the stack pushing the data of functionONE() below. Now the memory allocated for the function that was last in i.e.  functionTWO() is freed first, as the program exits  functionTWO() and the control is given back to  functionONE(). Stack is used for small data.

Unlike the stack, heap can be allocated and freed in any order. The size of the heap can increase or decrease dynamically based on the how much memory the program requires. It is slower to allocate as compared to allocations on stack. Heap is used when the data is much large, or the size of the data is unknown.

Primitives vs objects

In some programming languages, certain data types are defined as primitive data types. Primitive data types store small data with a fixed known size and the data is usually stored directly on the stack. For example, an integer variable in C# always takes 4 bytes of memory. Therefore, integers are primitives. Some other examples of primitive data in C# are float, double or boolean variables. On the other hand,  Python does not define any primitive types. Any data in Python is an object.

An object can be a more complex and abstract data structure that may consist of large amounts of data and functions. Objects are therefore stored on the heap. When a new object is created on the heap, the object’s reference is stored on the stack.  A reference can be thought of as an address which provides the location of the created object on the heap. Don’t worry if this is too much to digest right now, it will get clearer once we discuss an example below. Some examples of built-in object types in C# are strings, tuples, lists, arrays and dictionaries. In Python, as stated above, any data is an object and is therefore stored by reference.

Apart from the built-in object types, we can also create our own types using classes.


A class is defined using the keyword class followed by a name for the class.

C# code

We define a class named  Building with member variables  num_of_doors and  address. Member variables are also called attributes. Once the class has been defined, we can create new object of type  Building on the heap. In the above program, the variables  my_home and  my_school are reference variables (simply called references) that contain the addresses of the declared objects on the heap.

Python code

Whenever a new object of a class is created, the constructor function is automatically called. Therefore, we must provide any parameters that are required by the constructor function as arguments in the parenthesis. The purpose of the constructor function is to initialize the member variables of the class for each object. The constructor function in Python has the name  __init__() while in C#, the constructor function has the same name as the class. Notice that the each object of the class Building has their independent copies of the member variables and their values can be accessed using the reference and dot operator as we do in the print statements.

The public keyword makes the attributes of class Building accessible through its objects in the class Test. Without the public keyword we could not use the Console.WriteLine() function to display the my_home.address or  my_school.address. In Python, however, the attributes are always public and no keyword is required. The  this keyword in the constructor simply refers to the current object whose constructor is being called. It highlights the fact that the member variable that we are initializing is specific to this object.

It must be emphasized that the built in objects are also instantiated from classes. Any string object in C#, for example, is instantiated from the built-in “String” class. The “String” class contains all the attributes common to every string object and the functions that can be used with every string object.

Adding functions to our class

Classes can have member functions that can be called by any of its objects. The built-in “String” class, for example, includes a function called  ToLower() that we can use with any string object. The function returns the string in lowercase. Similarly, we can add member functions to our class that can be called by the class objects.

C# code

Python code

What we learnt

  1. The difference between stack and heap memory allocations.
  2. Object types and creating user defined object types using classes.
  3. Instantiating class objects and initializing member variables using constructor function.
  4. Adding and using class member functions.

< Functions               Inheritance and static members >


You may also like: