Sunday, 2024-04-28, 0:06 AM
Welcome Guest | RSS
Site menu
Tag Board
500
Our poll
How do you rate .Net classes ?
Total of answers: 208
Statistics

Total online: 1
Guests: 1
Users: 0

23-12-2010


Destructor: Destructor is a member of a class destructor will invoke automatically when an object is destroyed.

Distructor name should be same as class name but it should prefix with tilda(~).

Distructor will not return any value due to that reason it will not have the return types.

Distructor will have the destruction code.

Syntax:
    ~<classname()>
    {
          // Destruction code
    }


Eg: For Constructor:
namespace Constructorexample
{
  Public class Students
    {
      Public int i;
      Public Students()
       {
         i= 10;
         Console.WriteLine("Constuctor is calling");
       }
    }

  Class program
   {
      Void main()
        {
            Console.WriteLine("Before Object creation");
            Student obj = new Student();
            Console.WriteLine("After Object creation");
            Console.WriteLine("i value is " +obj.i);
            Console.ReadLine();
        }
    }
}
Output: Before object creation
             Constructor is calling
             After object creation
             i value is 10

According to number of parameters constructors are classified into 2 types:

1. Parameter less Constructor (or) default constructor:

A constructor which is not having any parameter then that constructor can be called as parameter less constructor or default constructor. Maximum a class can contain only one default constructor.

2. Parametrized constructor: A constructor which is having parameters than that constructor can be called as parametrized constructor.

If the constructor is having one parameter then it is called as one parameter constructor, if the constructor is having two parameter then it is called as two parameter constructor.

A class can contain multiple parametrized constructor but from one constructor to another constructor we should make the different with number of parameters.

Eg: For types of Constructor:


Eg: Of default Constructor:

Employee class is having two default constructor.

Public class Employee
{
  Public Employee()
   {
    }
  Public Employee()
   {
   }
}

The above code will generate an error because a class can contain only one default constructor.

According to modifiers constructors are classified into 2 types:
       1. Private Constructor
       2. Static Constructor


1. Private Constructor: While defining a constructor if we used private access modifier that constructor can be called as private constructor.

If a class is having private constructor the class can not be instantiated.

2. Static Constructor: While defining a constructor if we used "static" keyword, that constructor can be called as static constructor.

The purpose of static constructor is to initialize static variables.

While declaring a variable if we used "static" keyword that variable can be called as static variable.

Static & non static members: Class is a collection of members.

Class members are classified into 2 types:
        1. Static members
        2. Non-Static members


1. Static members: While declaring a class member if we used a static keyword that member can be called as static member.

Static members are called as class members because we have to access static members by using class name.

A class can contain static variable, static property, static method, static constructor, static event, but a class can not contain static indexers.

Syntax to access a static member:
    <classname>.<static member name>;


2. Non-static members: While declaring a class member if we didn't use "static" keyword that member can be called as non-static member & we can them as instance members also.

Non static members we have to access by using object name.

Syntax to access a non-static member:
    <objectname>.<non-static member name>;


Eg: For a static variables & non static variables:
namespace staticvariable
 {
   Public class myclass
     {
       Public static int i = 10;
       Public static int j = 20;
       // non static
       Public int k = 30;
     }

   Class mainclass
    {
      void main()
        {
           Console.WriteLine("Static variable i value is: {0}, j value is {1} ", myclass.i, myclass.j);
           myclass mc = new myclass();
           Console.WriteLine("Non static variable k value is " +mc.k);
           Console.ReadLine();
        }
    }
}
Output: Static variable i value is:10, J value is 20
              Non static variable k value is 30

Eg: For static method & non static method:
namespace staticmethods
{
  Public class myclass
    {
      Public static void method1()
        {
          Console.WriteLine("Static method is calling");
        }
      Public void method2()
        {
           Consloe.WriteLine("Non static method is calling");
        }
    }

  Class mainclass
     {
       void main()
          {
            myclass.method1();
            myclass.mc = new myclass();
            mc.method2();
            Console.ReadLine();
          }
     }
}



Search

Time
 

 

Calendar
 
«  April 2024  »
SuMoTuWeThFrSa
 123456
78910111213
14151617181920
21222324252627
282930

Twitter
 

Entries archive

Copyright R N Reddy © 2024Developed by Nagendra, Venkat, Vijaya and Yaswanth
Get free updates as SMS to your mobile. Just register by clicking here