Hi friends
today we will discussed about constructors and distractor in csharp
Constructors are used to memory allocation for real world entities which are created with the help of objects. When program or application is running, application required memory to save and manipulate data of that real world object/entities.
Now the question is how much memory is required for that object?
Frankly no one can decide how much memory is required for that application because of that all object oriented programming languages depends on heap memory.
Heap memory is a logical memory in RAM (temporary memory) and size is logically infinity (physically it is limited).This memory has to be creating at run time when we are using object in our program/application.
Now we have to
know that what type of data members we have in object.
In object we have two types of data members’
- object instance
- shared instance
Object instance
are allocated occupies object memory and every object have separate memory that
is isolated with others so to allocate the memory we have to call or invoke
functions which must be with implicit call.
All functions
are invoked explicitly for this reasons contractors are introduced.
Constructors are- Can be used to carry out the initialization of object members. These functions are the important part of object creation syntax
- are special member functions, with name of class , that enable an object to initialize itself when it is created
- Never return any value; therefore do not have return type, not even void.
- usually declared as public, however they can also be declared as private or protected. In such cases class cannot (instantiated) allocate memory for objects.
- The class cannot be used as a base/parent class for inheritance
[Accessing specifies] <classname>([arguments])
{}
Example
using System;
class Demo {
int x; int y;
public Demo(int i, int j)
{ Console.WriteLine("invoking two arg constructor");
x = i; y = j;
}
public Demo(int x) : this(x,x)
{Console.WriteLine("invoking the one arg constructor");}
public Demo() : this(0,0)
{
Console.WriteLine("invoking the default constructor");
}
public Demo( Demo d) : this(d.x, d.y) {
Console.WriteLine("invoking the one arg constructor - object");
}
}
class Test
{
public static void Main()
{
Demo d1 = new Demo ();
Demo d2 = new Demo (10);
Demo d3 = new Demo (10, 20);
Demo d4 = new Demo (d2);
}
}
We have different types constructors we have in Object Oriented Languages
- Instance constructor or default constructor
They are invoked only once during the entire life cycle of an object therefore can also be used to carry some initial task. Invoked for the memory allocation of an object and to initialize its members.
Takes care of the initialization of instance (non-static) member variables.Invoked only once during the entire life cycle of an object.Example:
Class rectangle {
Int length;
Int width;
Public rectangle()
{
Length = 0;
Width = 0;
}
Public int RectArea()
{
Return (length * width);
}
}
- Class or static constructor
In entire class we can create only one static constructor and that will declared with the help of static keyword. It is invoked when a class is loaded. The keyword static is used to differentiate the static constructor from the normal constructors. The static constructor can't take number of arguments. In other way it is not possible to overload a static constructor.
- Takes care of the initialization of class (static) data members.
- Declared using static keyword.
- A class can have only one class constructor.
- Invoked when a class is loaded.
- Cannot take any arguments. In other way it is not possible to overload a static constructor.
- Cannot specify access modifier.
- A given class (or structure) may define only a single static constructor.
- A static constructor executes exactly one time, regardless of how many objects of the type are created.
- A static constructor does not take an access modifier and cannot take any parameters.
- The run-time invokes the static constructor when it creates an instance of the class or before accessing the first static member invoked by the caller.
- The static constructor executes before any instance-level constructors.
class emp
{
public static int x;
static emp() { x = 10; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(emp.x);
Console.Read();
}
}
Example – Demonstrating class constructor
- Destructors
- A destructor is a method that is called when an instance goes out of scope
- Its name must be same as class name with no accessing specifies
- Used to clean up any resources
- Only classes can have destructors
- A destructor is a method that is called when an instance goes out of scope
- Its name must be same as class name with no accessing specifies
- Used to clean up any resources
- Only classes can have destructors
~Emp() { // destruction code }
In C++ distractor are written explicitly because memory has to be managed by programmer but in csharp and java this task will be take care by Garbage Collection, It is an Automatic Memory Management Scheme.
This will be invoked by virtual execution engine (java is JVM and MS.net is CLR) and Garbage Collector is a background process.
Task of garbage collector is cleans up the heap memory on your behalf when it’s no longer needed by automatically. GC traces object references and identifies objects that can no longer be reached by running code. The GC doesn’t take up processor resources by running constantly, but it invokes periodically.
You can force a garbage collection operation at any time with GC.Collect().
conclusion is
FAQS
- what is constructor?
- how many types of constructors you have in csharp
- what is static constructor?
- what is difference between static constructor and non static constructor?
- write syntax of constructor
- can i write static and private constructors in csharp
- how many ways we can de-allocate memory of objects in C#
- what is distractor and garbage collector
- which class will call to free the memory
thank you
Comments