Language Interoperability in .Net


In this article I have explained What is Language Interoperability and How it works in .Net. With the help of Language Interoperability you can interact with all Other Languages of .Net.With Interoperability you can interact with all the Languages in .Net .We can Save the Time instead of Generating the same Code Again and Again

What is .Net ?
1).Net is a Platform Independent Language which is used for developing various applications like Windows Applications,Web Applications ,Mobile Applications .
2).Net is a collection of more than 20 languages.Among them mostly used languages are C#.Net,VB.Net.

Though,.Net is collection of several languages Every language has its own compiler. VB.Net uses VB Compilier and C# uses C# compiler for Compilation.Every .Net Language after Compilation generates CIL Code.

What is CIL ?
CIL is the Common Intermediate Language which is generated after the Compilation of the Code.
After Compiling Vb.net code with its VB Compiler we get CIL Code.
After compiling C# code with its C# Compiler we get CIL Code.

CIL is the Intermediate Code which we get After the Compilation of the Source Code

What is Machine Code ?
Each Machine whether it is Windows or Unix consists of its own Software and Hardware . It consists of its own MicroProcessor. Every Machine will have some Instructions which can be understood by its own MicroProcessor .Those Instructions are nothing but Machine code(Machine Instructions)

What is CLR ?
Machine cannot Understand CIL Code which we get after the Compilation of a Program

CLR is used for converting the CIL Code into Corresponding Machine Code (Machine Instructions) which can be understand by its own MicroProcessor

What is Language Interoperability ?

 Code which is  written in any  .NET Language can be consumed in other .NET Language.This is  called as Language Interoperability . 
Every .Net Language after Compiling generates CIL Code which can be used by any other .Net Language i.e within .Net Each Langauage can perform Operations with other language with the help of CIL. This feature of.Net is nothing but Interoperability.

For Example , the CIL code which we get after compiling C# Language can be used by VB Language and the CIL Code which we get after compilation of VB code can be used by C#



How is this Possible ?
Though .Net is a collection of languages ,Every language has its own Data Types.C# has its own Data Types and VB.Net has its own Data Types but their sizes are same .After compilation any .Net language CIL Code will be generated which can be used by other Languages of .Net

Examples: After Compiling VB Code, Data Types in that Language will be Converted into CIL Type and it can be used C# and Vice-Versa

VB Datatypes converts to CIL which can be used by C#

VB --------> CIL --------> C#

Integer --> Int32 --------> int

Single --> Single --------> float

Boolean --> Boolean --------> bool

C# Datatypes converts to CIL which can be used by VB

C# ----> CIL --------> VB

Int --> Int32 --------> integer

Float --> Single --------> single

Bool --> Boolean --------> boolean


Language Interoperability is possible because even though DataTypes names are Different in Different .Net Languages their Sizes are same for all .Net Languages but after Compiling they will generate same CIL Code which can be used by any other .Net Languages

Every .Net Language after Compiling Generates CIL Code which can be used in other .Net Langauges

----Proof----

1)Take a New Project
2)Select Language as Visual Basic
3)Select Class Library Template
4)Save it as VB CODE

c



Why Class Library?
The code which is written in Class Library can't be used for execution but it can used be consumed in other classes.We must build the Class Library so that a dll file will be Generated.


Write the following code


Public Class Class1

Public Sub hitocsharp()
Console.WriteLine("Hi C# ")
End Sub

Public Function ADD(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
End Class


2

These are important Points
Since we cannot execute the code in Class Library, convert it into an Assembly
1)Open Solution Explorer
2)Right Click on Project i.e VBCODE
3)Select Build

3

How to use above VBCODE
1)Right Click on VBCODE
2)Select New Project
3)select Visual C#
3)Name it as CSHARPCODE

4

5

Right Click on Csharpcode and add a Class csharp

6

7

Now Add the Reference of VBCODE.dll which we have generated before
How to Add Reference files
1)Right Click on the Csharpcode
2)Select Add Reference
3)Browse VBCODE.dll in the VBCODE Project
4)Add VBCODE.dll to your Project

8

9

To access the VBCode we must add one statement i.e using VBCODE; because Now it is another Project

Write the Following Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VBCODE;
namespace CHARPCODE
{
class Csharp
{
static void Main()
{

Class1 obj = new Class1();
obj.hitocsharp();
Console.WriteLine( obj.ADD(1,3));
Console.ReadLine();
}
}
}



10

Now Execute the Csharp.cs

11


Observe that you will get the Output of the Class Library
Like this you can Perform Inter Operations between all the Languages in .Net.


Advantage Of Interoperability
With Interoperability you can interact with all the Languages in .Net
We can Save the Time instead of Generating the same Code Again and Again

Note that Language Interoperability can be possible only with the help of CLS(Common Language Specifications) and CLR(Common Language Runtime) of .Net


Comments

Author: mallikarjun3409 Feb 2014 Member Level: Bronze   Points : 0

Hi Reddy,
Thanks for posting such a nice article about Interopability



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: