Ad Code

Responsive Advertisement

Language Interoperability and Destructors:

Language Interoperability and Destructors

Open visual studio à go to file new  Project  à choose console application à name it as accessDeemo1 and name the solution as MySolution , by default we get a class in the project as program,

write following code in it making the class as public :
    public class Program
    {
       private void Test1()
        {
            Console.WriteLine("Private Method");
        }
       internal void Test2()
       {
           Console.WriteLine("Internal Method");
       }
       protected void Test3()
       {
           Console.WriteLine("Protected Method");
       }
       protected internal void Test4()
       {
           Console.WriteLine("Protected Internal Method");
       }
       public void Test5()
       {
           Console.WriteLine("Public Method");
       }
       static void Main(string[] args)
       {
           Program p = new Program();
           p.Test1(); p.Test2(); p.Test3(); p.Test4(); p.Test5();
           Console.ReadLine();
       }
    } 

- Add a new class Two.cs in the project and write the following:
    class Two : Program
    {
        static void Main()
        {
            Two obj = new Two();
            obj.Test2(); obj.Test3(); obj.Test4(); obj.Test5();
            Console.ReadLine();
        }
    } 

- Add a new class Three.cs in the project and write the following:
    class Three
    {
        static void Main()
        {
            Program p = new Program();
            p.Test2(); p.Test4(); p.Test5();
            Console.ReadLine();
        }
    } 
- Add a new project under the solution of type console application à name it as AccessDemo2 and rename the default file Program.cs as Four.cs , so that class name also changes as Four à add the reference of AccessDemo1 assembly from its physical location to the current project & then write the following code :
    class Four : AccessDemo1.Program
    {
       static void Main()
        {
            Four obj = new Four();
            obj.Test3(); obj.Test4(); obj.Test5();
            Console.ReadLine();
        }
    } 
- Add a new class under the Project AccessDemo2 names it as Five.cs and write the following:
using AccessDemo1;
using System;
namespace AccessDemo2
{
    class Five
    {
        static void Main()
        {
            Program p = new Program();
            p.Test5();
            Console.ReadLine();
        }
    }
} 

Language Interoperability

As discussed earlier the code written in 1 .net language can be consumed from other , to test this add a new project under the MySolution by choosing the language  as VB and template as class library which is collection of types that can only be consumed but not executed .

- The project comes with a default class Class1 in the file Class1.vb write the following code init.
Public Class Class1
    Public Sub Add(ByVal x As Integer, ByVal y As Integer)
        Console.WriteLine(x + y)
    End Sub
    Public Function Say(ByVal name As String) As String
        Return "Hello" & " " & name
    End Function

End Class 

- Now to compile the project open solution explorer à right click on the VBproject à and select Built which compiles and generates an assembly.

- Now add a new class under AccessDemo2 project as TestVb.cs , add the reference of VBProject’s assembly from its physical location and write the following code:
using VBDemo;
using System;

namespace AccessDemo2
{
    class TestVB
    {

        static void Main()
        {
            Class1 obj = new Class1();
            obj.Add(100,50);
            Console.WriteLine(obj.Say("Prabhu"));
            Console.ReadLine();
        }
    }
}

Members Of a Class

As we are aware a class is a collection of members which can be of various types like –

<!--[if !supportLists]-->1.       <!--[endif]-->Fields (variables)

<!--[if !supportLists]-->2.       <!--[endif]-->Methods

<!--[if !supportLists]-->3.       <!--[endif]-->Constructor

<!--[if !supportLists]-->4.       <!--[endif]-->Destructor

<!--[if !supportLists]-->5.       <!--[endif]-->Properties

<!--[if !supportLists]-->6.       <!--[endif]-->Indexers

<!--[if !supportLists]-->7.       <!--[endif]-->Events

<!--[if !supportLists]-->8.       Delegates

<!--[if !supportLists]-->9.       <!--[endif]-->Ename

  Destructors

   These are also special methods, avilabel in a class similar to a constructor, whereas a constructor method gets executed whenever the object of the class is created and destructor method gets executed whenever the object of class is destroyed.

Both Constructor and Destructor will have the same name i.e. name of the class, but to distinguish between these two we use tilled operator (~) before of destructor.
class Test
{
Test()
{
//Constructor
}
~Test()
{
//Destructor
}
}

- A constructor method can have modifiers as well as parameters also, whereas, a Destructor cannot have it.

-Add a class TestDemo.cs under OOPSProject and write the following:
using System;
namespace oopsProject
{
    class DestDemo
    {
        public DestDemo()
        {
            Console.WriteLine(" object 1 created");
        }
        ~DestDemo()
        {
            Console.WriteLine("object 1 distroyed");
        }
    }
    class DestDemo2 : DestDemo
    {
    public DestDemo2()
    {
        Console.WriteLine(" object 2 created");
    }
         ~DestDemo2()
        {
            Console.WriteLine("object 2 distroyed");
        }
         static void Main()
         {
             DestDemo2 obj = new DestDemo2();
             //obj = null ;    GC.Collect();
             //Console.ReadLine();
         }
    }
} 
- As we are aware Garbage Collector is responsible for memory management i.e. allocation and dislocation, so it is only responsible to create an object and allocate the memory and destroy the object and de-allocates the memory.

- Garbage Collector comes into the picture for destroying the object of a class in different case like:


<!--[if !supportLists]-->1.       <!--[endif]-->In the end of a programs execution, when the program gets executed successfully without any shortage of memory.


<!--[if !supportLists]-->2.       <!--[endif]-->Sometimes it can also come into picture in the middle of a programs execution provided there is a memory shortage, which identifies all unused object to destroy them.


<!--[if !supportLists]-->3.       <!--[endif]-->We can also explicitly call the Garbage Collector in to a program in the middle to perform the de-allocation process , but this is not advised because, whenever Garbage Collector comes into picture for performing de-allocation it suspends the execution of program for an unpredictable laps of time.

-Execute the above program using ctrl F5 and check the result. In this case we find both constructor and destructor getting called one after the other because the main method contains only single statement in it i.e. is creating object of class, so when object is created constructor is called and because that is end of execution of program object get destroyed and destructor gets called.

Note – When constructors are called in case of inheritance they adopt top to bottom approach, whereas in case of destructor it will be bottom to top approach.

-  Now uncomment the ReadLine statement in main and execute the program again using ctrl + F5, now we can see only constructor getting called but not destructor because the program is still under execution at ReadLine statement , press enter which will end the program and invokes the destructors.

-  Now uncomment the second statement also in Main and execute the program again , where we can see destructor getting called before program execution is completed only because we are explicitly calling the Garbage collector in to the program to destroyed unused object.
Reactions

Post a Comment

0 Comments