How to drastically bring down the memory usage through interface?

Vishal Pathak
Dec 18, 2021 · 3 min read

Have you been in any situation, where you don’t want to use some functions of the class but then also that function is occupying the space in the Ram through object instantiation. If yes, then you are at the right place 🙌.

Photo by Fredy Jacob on Unsplash

Introduction:-

There are many ways we can lower down the space utilization but by using interface is the most simple and recommended way. So let’s first understand what is interface.

Interface:-

An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface.

In this blog we will only discuss interface in C# but the solution is applicable in all the OOP languages.

Class Instantiation:-

Now let’s understand how memory is getting allocated to the class properties through instantiation of the class. So when we instantiate class we provide object name and that object is holding the memory address of the class properties. So now if we don’t use the Interface in the program then all the properties of the class will occupy the memory space which is not recommended.

So to resolve the above issue we will use multiple inheritance through interfaces.

ISchool Interface:-

In the below Screenshot two methods are declared i.e getSchoolName and getSeatsAvailable.

ICollege Interface:-

In the below Screenshot two methods are declared i.e getCollegeName and vacanciesAvailable.

Education Class:-

In the below class both the interfaces are implemented. Here we have also demonstrated you the multiple inheritance in C#.

MainProgram class:-

In MainProgram class we have instantiated class Education for both the interfaces individually. And it is also visible that compiler is not showing the suggestion for the methods which are declared in ICollege interface because the object is of type ISchool interface. So at the time when application is running then the object will hold only two methods in the memory.

Bhagavad Gita

यज्ज्ञात्वा न पुनर्मोहमेवं यास्यसि पाण्डव |

येन भूतान्यशेषेण द्रक्ष्यस्यात्मन्यथो मयि || 35||

BG 4.35: Following this path and having achieved enlightenment from a Guru, O Arjun, you will no longer fall into delusion. In the light of that knowledge, you will see that all living beings are but parts of the Supreme, and are within me.

Abhima C# Programming

Posts about C# programming language and ASP.NET technology

Abhima C# Programming

This publication provides articles about C# programming language and ASP.NET technology. We posts a article weekly.

Vishal Pathak

Written by

love ❤ coding, solving some industry problems technologies: JavaScript, C#, Angular, PLSQL, Docker Want to learn: Python, Go language, AI, ML and Cloud

Abhima C# Programming

This publication provides articles about C# programming language and ASP.NET technology. We posts a article weekly.