Purpose of new Keyword

By Vidya Vrat Agarwal Mar 25, 2013
Explains the purpose of new keyword.

When you see such statement, one thing which comes in your mind about purpose of new keyword is that it creates an instance, isn't it?

Class objC= new Class();


When this is certainly the main purpose, there are other behind the scene tasks our well known new keyword is responsible for, lets understand what:

1-  Verifies the size needed for object allocation on managed heap.
2- If sufficient space is available then allocate the object, where application root pointer is pointing.
3- If required space is not available then, it triggers the GC (Garbage collection) lives in System.GC namespace, which does the heap cleanup and then after reclamation of needed space, the object will be allocated.

So, new is not just instance creation, its more that that. By the way, IL instruction for new is newobj.

Vidya Vrat Agarwal

.NET Passionate, TOGAF Certified Architect, Certified Scrum Master (CSM), Author, Microsoft MVP , C# Corner MVP, Speaker, Certified MCT, MCPD, MCTS, MCSD.NET, MCAD.NET, MCSD and lifetime member of the Computer Socie... Read more

Personal Blog: http://www.MyPassionFor.NET
COMMENT USING