Introduction to Classes:

text zoom

Classes are the core of OOP's. Classes are declared by using the keyword classes. Classes contain data members that represent the class. We can create object of the class, objects are entities and we can create as many number of objects of a given class.


 


Syntax:


Class classname {


//Variable declaration


// Method declaration


}


 


Classes can contain the following entities inside its declaration:



  • Constructors

  • Destructors

  • Fields

  • Methods

  • Properties

  • Indexers

  • Delegates

  • Events

  • Nested Classes.


 


Constructors:- Constructors are called or initialized automatically whenever an object of the class is created. The purpose of the constructors is to initialize the class members whenever an object of the class is created.



  • Constructor name is same as of class name.

  • Constructor doesn't return any value, nor even void

  • Constructor initialize the class members at the time of its creation


There are different types of constructor:



  • Default Constructor

  • Parameterized Constructor

  • Copy Constructor


 


Destructor:- Destructor is used to deallocate any memory that is allocated to the object at the time of its creation. Destructor is used for memory management and avoids any memory leaks that occur in the case of reference variables that store on heap memory.


Destructor keeps tracks of all the unwanted and unused memory and frees it when we are not using it.



  • In .net the memory management is automatically done through GC i.e. garbage collection, this is responsible for deallocation of memory.

  • The reference variable memory allocated on heap and here the job of GC comes into picture, it will deallocate the memory that is not in used.

  • The value type variables allocated memory on stack, here the variable doesn't require any GC, they just fall from the stack if there are not in used.


 


Example: Demonstrate Constructor, Destructor and used of Static method .


 




 


 


TestDemo.cs


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace cSHARPEXAMPLES


{


  public class TestDemo


    {


      String str;


    static String str2 = "default" ;


    public TestDemo() {


    //Constructor


        str = "Hi" ;


        }


    public TestDemo( String strdefault) {


    //Parameterized Constructor


        str = strdefault;


        }


    public String display(){


      return str;


        }


    public static String staticfx() {


      return str2 + " This comes from Static fx" ;


        }


        ~TestDemo() {


    // Clean Up Code Here.


        }


    }


}


 


Form6.cs


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


 


namespace cSHARPEXAMPLES


{


  public partial class Form6 : Form


    {


      public Form6()


            {


              InitializeComponent();


            }


      private void button1_Click( object sender, EventArgs e)


            { //Calling Default Constructor


                TestDemo obj = new TestDemo ();


        MessageBox .Show( "default Constructor :: " + obj.display());


            }


      private void button2_Click( object sender, EventArgs e)


            { //Calling Parameterized Constructor


                TestDemo obj1 = new TestDemo ( "Vikrant" );


        MessageBox .Show( "Parameterized Constructor :: " + obj1.display());


            }


      private void button3_Click( object sender, EventArgs e)


            { //Calling Static method of the class


          MessageBox .Show( "Static method :: " + TestDemo .staticfx());


            }


        }


}


 


Output:


 


Click on: Default Constructor Button




 


 


 


 


 


Click on: Parameterized Constructor Button


 




 


Click on: Static method Button



                    

Copyright © 2013 VisualBuilder. All rights reserved