This article presents some interesting facts of the new operator in C#. This is part 2 of the Look Under the Hood Series.
Dear friend, how are you? I am fine though a little busy with my MCA final semester exam. Anyway let's start our journey of "Under the Hood Concepts". Here is my Series-1. This article presents a practical explanation of how the new operator works in the C# .NET environment. And I know the new operator is not new to developers. (Why new? You did not use in C++ to create objects?) Yes, the new operator creates objects of a class. Object or Instance?? There is a little confusion about what objects and instances are, right?
Let's clear clarify Objects and Instances
My little experience says that this first confusion exists when people start learning Object Oriented Programming. I too was among them. I asked my teacher "Sir! What is the difference between an object and an instance?" And he replied "An instance is something that is waiting to be an object of a class".
Then, I began to determine the exact difference between them. Let me explain in my way.
Instance: An instance is a generic thing (I will not say generic object to define a generic thing because you may say I am using object to define instance, Ha Ha...) that will be transformed into an object if we set some property to it.
Object: An object is something that holds it's own property and own behavior.
I know you are not satisfied with this rubbish definition. Then let me use my last weapon (Ha Ha...). Yes let's understand with an example. Have you used LINUX or UNIX in your college lab?
If yes then read few more line and you will clearly understand objects and instances. Let us consider a UNIX server as my Class. And all the computers in the lab are accessing the same server. The cursor in the login screen is blinking in all the computers but no one (no student) has entered their username and password because they don't know the username and password and they are waiting for the system admin. Now, if there are 60 computers in the lab (in our lab it's 60) then we have created 60 instances of the Class (here UNIX OS).
Now after a while the Admin comes and announces that the system username is their name and their password is their roll number. Immediately all students login to the system and I am observing my friend, who is in my right chair, has set his font color to Red and left the chair as Green. Hmm. They are customizing thier environment. And when they are customizing their terminal with their own property each and every terminal is transforming to an object.
I think now you are clear with object and instance. Now let's proceed with our discussion.
How memory get allocate using new operator?
Yes, the new operator creates objects of a specified class. Here is my code, I have defined one class and I have created just instance of the class. And in the IL code you will see there is no sign of the object anywhere in the IL code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
namespace Test1
{
class Test
{
}
class Program
{
static void Main(string[] args)
{
Test t = null;
Test t1;
}
}
}
Here is the IL code.

Now let's use the new operator and see the change.
The following is my C# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
namespace Test1
{
class Test
{
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
}
}
}
And the IL output is here.

We can see very clearly that the new object has created one object of my Test class. Now after reading to this point if you draw the conclusion in your mind "Ok , I understood that the 'new' operator allocates memory" then you are partially wrong. Let's get to the next point.






jubula samalPosted May 24, 2019, 4:51 AM
Really I appreciate you the way you have described it and makes a lot of concepts clear. One doubt so please make me clear on this below point. int[] a = new int[10]; - memory allocation does not hapen because ode optimizer sees that there is no use of an array in the current program, it simply omits the array from being processed. Fine so far... But when we do Test t = new Test(); why memory allocation happens even we are not using this instance/object anywhere in current programm.
Saiprasad ChinnaPosted Aug 22, 2017, 7:09 AM
Good concept..................
Munesh SharmaPosted Jan 18, 2017, 6:27 AM
It is a nice explanation on memory allocation
Anjali KumariPosted Aug 15, 2016, 9:12 AM
Can You check the statement where you return bull ( If your data type is string then it will be "bull" by default. )instead of null and can Explain me What is Dynamic memory
ankur goyalPosted Feb 15, 2016, 12:03 PM
superb blog....
Mitu NayakPosted Dec 18, 2015, 2:09 AM
Nice Blog
manoj chandra joshiPosted Oct 20, 2014, 5:57 AM
awsome
deepak gondPosted May 20, 2014, 3:27 AM
nice article