Skip to main content

OOPS-Constructors and distractor





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’
  1. object instance
  2.  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
Systax:
[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


Writing 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

  1. what is constructor?
  2. how many types of constructors you have in csharp
  3. what is  static constructor?
  4. what is difference between static constructor and non static constructor?
  5. write syntax of constructor
  6. can i write static and private constructors in csharp
  7. how many ways we can de-allocate memory of objects in C#
  8. what is distractor and garbage collector
  9. which class will call to free the memory

thank you

Comments

Popular posts from this blog

FAQS on DOTNET

Hello to every one who are fallowing my blog, today i attend one call from one Technical Recruiter (name is not required i think ) he asked some couple of question...ok i answered ....mean while  he asked one question (in my life never i feel it is relevant  for developer) who will use oops development environment? ha.. ha.. expect answer and update  to me   So far we received many request for FAQs on CSHARP, ASP.NET AND SQLSERVER for fresher who are in finishing schools. We are actively working with these Technologies  to bring   KEY   Resources for   TECHIES.                                   you are interested in helping others  bring  TECHKEYS   to your Friends through Social Media, please Leave the Comment to us to improve our work and subscribe  for more information .   Q 1) levels of nesting in a subquery?      a)2        b)6         c)32        d)64 Q 2) static method correct syntax:      a.private static main()                            b.public stat

SQLSERVER Functions- Coalesce()

SQL  Server  built-in functions Coalesce() It is A function Is Very Much Useful at Run time When We Have Multi Value Attribute in a Table. If U Consider below facts placed in a employee table With Id, Name, ph_no, Alt_no, Office no. id Name Ph_ no Alt_ no Office no 101 Albert 999999 456453 321333 102 khan null null 123455 103 victor 112121 null null 104 lovely null null 1897321 Above Employee table may have single value are three values if it have single value then it fill null  values with remaining attributes When We Retrieve The Number from employee table That Number Should Not be Null value To get not null value from employee table We Use Coalesce() Function It Returns First Encountered Not Null Value  from employee table select id , name , coalesce ( Ph_no , Alt_no , Office_no ) as contact number from employee