Advertisement

Constructors and Destructors

By Bintu Chaudhary   Posted at  5:41:00 AM  

Home  »  C#  »  Constructors and Destructors


     The accessing of various members/objects of the class comparison of class with structures and use of classes using special member function called Constructors and Destructors for initializing and disposing of objects belonging to that class.

Contents

Constructors

     Generally a Constructor is that which is used for initializing the data Members of a class and a Constructor is that whose name is Same as name of class and used for Initializing the data members of Class and it gets Automatically called when an object of class is created But Always Remember Some things about Constructor Like :-

  1. Name of Constructor must be same as name of class
  2. Have No Return Type Even Not Void
  3. Automatically Called when an object of class is created
  4. Used for Assigning Some Values for data Members or variables

There are four types of Constructor as follows:-
  1. Default Constructors
  2. Parameterized Constructors
  3. Copy Constructors
  4. Static Constructors

Default Constructor

     Default Constructor is also called as Empty Constructor which has no arguments and It is Automatically called when we creates the object of class but Remember name of Constructor is same as name of class When a user creates an object of class with the help of new operator then Constructor gets called.




Parameterized Constructor

     This is Another type Constructor which has some Arguments and same name as class name but it uses some Arguments So For this We have to create object of Class by passing some Arguments at the time of creating object with the name of class and The Name of Parameter also be unique Means that name will not be Used by anyone.




Copy Constructor

     This is also Another type of Constructor. In this Constructor object of another Constructor is passed As name Suggests you Copy means Copy values of another Class object This is used for Copying the values of class object into an another object of class So For Calling Copy Constructor We have to pass the name of object whose values we wants to Copying




Static Constructor

     As we Know that Constructor gets Automatically Called when an object of a Class is Created For this we must have to create an object so that Static Constructor is the Special type of Constructor which is used for Assigning values to data members and without Creating an Object a user can Access Data Member or Variable of Class. For Calling a data Member from a Class a user can just use the Name of Class and name of data Member directly.




Destructors

     As we know that Constructor is that which is used for Assigning Some Values to data Members and For Assigning Some Values this May also used Some Memory so that to free up the Memory which is Allocated by Constructor , destructor is used which gets Automatically Called at the End of Program and we doesn't have to Explicitly Call a Destructor and Destructor Cant be Parameterized or a Copy This can be only one Means Default Destructor which Have no Arguments. For Declaring a Destructor we have to use ~tiled Symbol in front of Destructor.




Constructor Overloading

When a Constructor has a Same Name but he has a different behaviors then this is called as the Constructor Overloading The Constructor Overloading is Occurred when :-
1) Name of Constructor is Same.
2) Different Number of Arguments Passed to Constructor.




Method Overloading

     Method Overloading is also Called as Function Overloading. Overloading Means a Functions has many Behaviors occurred When in class when a functions has same name but different behaviors A Functions said to be overloaded When For Calling a Method or a Function is Overloaded we have to Remember few things:-

1. Function has same Name but Different Return Type.
2. Difference in No of Arguments.
3. Different Return Type in Arguments.

When We Pass a Call for Execution then it will match the Criteria of Function Like Number of Arguments and Data types etc This is the Responsibly of Compiler to Find the Exact Match for Constructor and When a Compiler Finds More than One Match Then this will gives us an Error Message.

Static Members

     We know in Class data members and Members Functions are Instantiated, When Object of Class is Created New Memory is Allocated by Each Object .So for Avoiding this New Memory Each Time Creation We Uses Static Data then data will be Common to all and the static data is used without creating an object of class. Static data is used without creating an object Instead with name of class. The Members those are declared as Static are Known as Static Members are associated with the class itself rather than individual objects Main Advantage of Static variables is C# creates only one Copy of Static data Members in Memory Like Data Members Methods May also be Static and they Called without object of class Always Remember that Static data Members if are not Initialized then they have automatically initialize to Zero .

THIS Reference

     When a user have to face Problem to use same name of Variables in a class and Variables of Function or you can say that when a user wish to use Same name of Variable for Removing the Problem to Remember Names of Many Variables Then he May use THIS Keyword . THIS Reference is used for Specifying the Difference Between the Function Variables and Original Variables.

Read-Only Members

     We knows that data of Class is Accessed by Any user when they are declared as Public. But if we wants to Prevent the value of data Member of a Class or in other Words if we doesn't Want to Change the Value from outside the Main Then we can Declare a Data Member or a Variable as a Read-only in a Class . Once Declared as Read-only they we can't Make Changes into the Variable




Properties

     As we Know that Data Which is Private in a Class will not be Accessible from outside the Class then this Problem can be Solved with the Help of Property which is same as like a Friend Function as in C++ But in C# Properties are used for Specifying Values into the Private data and For Using or Making a Property a user have to use Set and get Keywords and also a value Keyword The Value Which is Assigned to a Property will be Given to that Private data Member and then we may Retrieve a Value from that Property.

Indexers

     Indexers are also Same as Properties but they are used for Specifying or to Set the Values or Private Data Members through an index Number when we are Indexer then we have to use the this Keyword and also we have to use set and get Keywords for Specifying the Value into the Private Variables As I an Saying that a user have to Specify the index also So for this a User have to use the Collection Element Either it May be a List or Either this May a Array List.






About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

Back to top ↑
Connect with Me