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.
Object-oriented programming (OOP) is the core ingredient of the .NET framework. OOP is so important that, before embarking on the road to .NET, you must understand its basic principles and terminology to write even a simple program. The fundamental idea behind OOP is to combine into a single unit both data and the methods that operate on that data; such units are called an object. All OOP languages provide mechanisms that help you implement the object-oriented model. They are encapsulation, inheritance, polymorphism and reusability. Let’s now take a brief look at these concepts.
- OOP’s overview
- Classes and Objects
- Constructor and Destructor
- Function Overloading
Access modifiers are keywords (private, public, internal, protected and protected internal) which are used to specify the accessibility of a type and its members.
// Available only to the container Class
private string privateVariable;
// Available in entire assembly across the classes
internal string internalVariable;
// Available in the container class and the derived class
protected string protectedVariable;
// Available to the container class, entire assembly and to outside
public string publicVariable;
// Available to the derived class and entire assembly as well
protected internal string protectedInternalVariable;
private string PrivateFunction()
internal string InternalFunction()
protected string ProtectedFunction()
public string PublicFunction()
protected internal string ProtectedInternalFunction()
Now to demonstrate the behaviour how these class members are exposed to another class depending upon their scope defined via modifier/specifier and when we create an object or inherit from the above created class named “AccessModifier”
As mentioned in the above shown code and before each member variable I wrote a comment which describes the access level of each type of member varaible.
Soif we derive another class “CallAccessModifier” from the parent class “AcessModifier” then “private” type members will not be visible because its considered as outside the scope of parent class.
But “protected” members are the ones which become available only during the inheritance (when a child is derived from a parent class) as shown via image below.
|Showing class members availability through inheritance.|
Note:- “this” is a keyword referes to the current instance of the class and used to access members. In the image above this keyword shows all the available members with the classs.
If you notice in the above shown image it clearly shows all the protected members are visible due to virtue of inheritance, along with other members with scope defined as internal, public and protected internal.
Now let’s see what happens if we decide to create an object of the class “AccessModifier” shown in the code above. As per the rule private and protected must not be visible via an object:
|Showing members available when an object is created of the given class|
Now as shown in the image just above we are not able to see private and protected members because they both are not exposable via an object.
Besides we are able to see protected internal member, and this is correct because its a mix of both protected and internal and so exposed in here because we are in the same assembly.
Q. What happens if we build a .dll of the “AccessModifier” code and use it in other project.
A. The rules remain the same, once AccessModifier.dll is refered in other project its considered as outside ot the current assembly and so:
** private members are not exposed
** internal members are not exposed