Searching...

Translate

Destructor

v Destructor

   It is possible to define a method that will be called just prior to an object’s final destruction by the garbage collector. This method is called a destructor. The objects are automatically destroyed by garbage collector.



   You might use a destructor to ensure that a system resource owned by an object is released.

   Thus, a destructor is declared like a constructor except that it is preceded with a ~ (tilde). Notice it has no return type and takes no arguments.

   To add a destructor to a class, you simply include it as a member. It is called whenever an object of its class is about to be recycled. Inside the destructor, you will specify those actions that must be performed before an object is destroyed.

   It is important to understand that the destructor is called just prior to garbage collection. It is not called when a variable containing a reference to an object goes out of scope


Þ   Example

using System;



    class test
    {
        public int x;
        public test()
        {
            x = 10;
        }
        public test(int n)
        {
            x = n;
        }
        ~test()
        {
            Console.WriteLine("DESTROYING OBJECT");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            test t1 = new test();
            test t2 = new test(12);
            Console.WriteLine("t1.X = {0}", t1.x);
            Console.WriteLine("t2.X = {0}", t2.x);
            Console.ReadLine();//When you are showing output then
                                       press enter after than
            //you can see "DESTROYING OBJECT" written two times. or
                     run with CTRL+F5.

        }
    }

   Output:-

t1.x=10
t2.x=12
<press enter>
DESTROYING OBJECT.
DESTROYING OBJECT.


*   2.4 Arrays (one dimensional, multidimensional, jagged)

   An array is a collection of variables of the same type that are referred to by a common name. In C#, arrays can have one or more dimensions, although the one-dimensional array is the most common.

   The principal advantage of an array is that it organizes data in such a way that it can be easily manipulated. Also, arrays organize data in such a way that it can be easily sorted. Although arrays in C# can be used just like arrays in many other programming languages.

v One-Dimensional Arrays

   A one-dimensional array is a list of related variables. Such lists are common in programming. For example, you might use a one-dimensional array to store the account numbers of the active users on a network.

   To declare a one dimensional array, you will typically use this general form:

SYNTAX :- type[] array-name = new type[size];

   Here, type declares the element type of the array. The element type determines the data type of each element that comprises the array. Notice the square brackets that follow type. They indicate that a one-dimensional array is being declared. The number of elements that the array will hold is determined by size.

   Here is an example. The following creates an int array of ten elements and links it to an array reference variable named sample. The sample variable holds a reference to the memory allocated by new. This memory is large enough to hold ten elements of type int.

int[] sample = new int[10];

         Or

int[] sample;
sample = new int[10];

   An individual element within an array is accessed by use of an index. An index describes the position of an element within an array. In C#, all arrays have 0 as the index of their first element.

Þ   Example
using System;

class ArrayDemo
{
static void Main()
{
int[] sample = new int[10];
int i;
for(i = 0; i < 10; i = i+1)
sample[i] = i;
for(i = 0; i < 10; i = i+1)
Console.WriteLine("sample[" + i + "]: " + sample[i]);
}
}
   The output from the program is shown here:
sample[0]: 0
sample[1]: 1
sample[2]: 2
sample[3]: 3
sample[4]: 4
sample[5]: 5
sample[6]: 6
sample[7]: 7
sample[8]: 8

sample[9]: 9

0 comments:

Post a Comment

Subscribe us