Cross-Language Interoperability in .NET Framework

The .NET Framework is language-independent. We can develop many languages targeting the .NET Framework, such as C#, C++, F#, Visual Basic and Windows PowerShell.

In simple words, a function, class or anything written in one language can be easily used in another language. (For example a function, class or anything written in C# can be easily used in VB.NET).

We're using 2 languages for our example, VB .Net and C#.

Let's look at an example.

What we'll do is, we'll create a Class Library file in C# (in other words  a DLL file) and will use that DLL file in a Visual Basic application.

Let's start. First create a Class Library Project in C#.

Class Library Project
Here we're going to create a “CalculatorClassLibrary” that contains some public functions, so that they're accessible by an end application.

Create a new class called Calculator with its functions.

Calculator.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace CalculatorClassLibrary  
  7. {  
  8.     public class Calculator  
  9.     {  
  10.         public static double Addition(double number1, double number2)  
  11.         {  
  12.             return number1 + number2;  
  13.         }  
  14.   
  15.         public static double Subtraction(double number1, double number2)  
  16.         {  
  17.             return number1 - number2;  
  18.         }  
  19.   
  20.         public static double Multiplication(double number1, double number2)  
  21.         {  
  22.             return number1 * number2;  
  23.         }  
  24.         public static double Division(double number1, double number2)  
  25.         {  
  26.             double ans = 0;  
  27.             if (number2 == 0)  
  28.             {  
  29.                 ans = 0;  
  30.             }  
  31.             else  
  32.             {  
  33.                 ans = number1 / number2;  
  34.             }  
  35.             return ans;  
  36.         }  
  37.     }  

Now Press F6 to build our class Library.

Once our Class Library has been successfully built, right-click on Project in Solution Explorer and select “Open Folder in Windows Explorer”, in our bin\Debug folder we'll get out DLL file as shown below.

dll file

Now add a New Project by clicking on the Solution as shown below.

Add New Project

Now we'll create a Visual Basic Project to do Language Interoperability.

Language Interoperability

In the Add New Project window, go to Other Language and select Visual Basic.

Select Console Application and give it a proper name.

Now what we'll do here is, we'll use that Calculator.dll file to do operations.

Add “CalculatorClassLibrary.dll” into your reference as shown below.
CalculatorClassLibrary
Go to the bin\Debug folder of your Class Library and select the DLL file as shown below.

Debug folder

Like C#, Visual Basic allows you to list each namespace used within the current file.

Visual Basic offers the Imports keyword rather than C#'s using keyword. Add the following Imports statement within the Module.vb code file.

Module1.vb

  1. Imports System  
  2. Imports CalculatorClassLibrary  
  3.   
  4. Module Module1  
  5.   
  6.     Sub Main()  
  7.         Dim addition As Double  
  8.         Dim multiplication As Double  
  9.         addition = Calculator.Addition(10, 20)  
  10.         multiplication = Calculator.Multiplication(5, 2)  
  11.         Console.WriteLine("Addition is: {0}", addition)  
  12.         Console.WriteLine("Multiplication is: {0}", multiplication)  
  13.   
  14.         Console.ReadLine()  
  15.     End Sub  
  16.   
  17. End Module 

As you can see our VB.NET code is showing access to our C# Class Library Methods as shown below.

Class Library Methods

Everything is now done.

Set the Startup Project to MyVBApplication as the default project.

To do this right-click on your project then select Properties, you'll get the following window where you can set "Single Startup Project" as shown below.

Single Startup Project

Click Ok and Press F5 and your VB Project will contain a DLL file that is written in C# without any error or warnings. After a successful run you'll get your output.

output