- Introduction
- C#
- Visual Basic .NET
- Visual J#
- Managed C++
- Other .NET Programming Languages
- Interoperating with Different Programming Languages
Interoperating with Different Programming Languages
The objective of this section is to illustrate that .NET really supports cross-programming language use. You will learn how classes defined in one programming language can be subclassed in another and utilized in yet another. First, define the Base class in C#.
using System; namespace mixed { public class Base { public Base() { } public virtual String GetLanguage() { return "C#"; } } }
This program is compiled into a library using the C# compiler.
csc /t:library CSharp.cs
Next, a Visual Basic .NET class is declared as a subclass of the Base class.
Imports System Namespace mixed Public class Derived Inherits Base Public Sub New() End Sub Public Overrides Function GetLanguage() As String return "Visual Basic .NET" End Function End Class End Namespace
To compile the Visual Basic program, the Visual Basic .NET compiler is invoked, passing the reference to the previously created base class assembly.
vbc /reference:CSharp.dll /target:library VBasic.vb
Now you eventually develop a Java class that actually uses the classes defined earlier. The Java class has a main method and so can be compiled into an executable.
vjc /reference:CSharp.dll,VBasic.dll VJSharp.jsl
The program VJSharp.exe can be executed to get the results.
Visual Basic .NET Derived is an instance of Base
This small exercise should remove any doubts regarding the .NET interoperability of the various programming languages.
Selecting the Right Programming Language
The .NET programming model provides developers a true choice when selecting a programming language to implement an application or when creating a component library. Selecting the language to use primarily depends on your existing skills and is your personal choice. If you are a Visual Basic programmer, you will probably pick Visual Basic .NET. If you are a C++ programmer, you will either continue with Visual C++ .NET or consider C# as your major programming language. If you have come from a Java programming background, you will probably end up with either Visual J# or Visual C#. Another factor that would influence the choice of programming language is ubiquity and market demand in your area. For instance, currently the New York City metropolitan area (where I live) seems to be headed in the C# direction, based on its high C++ influence. Although it is definitely possible to mix different programming languages in a single solution, in my opinion, it is rarely done. In many ways, different programming languages can be utilized to achieve similar results; sometimes the choice of a language is influenced by what sort of application is being written. For instance, Perl might be an excellent candidate for a bunch of .NET components that do heavy pattern matching and text processing. In a nutshell, when you select a programming language, you should look at your existing skills and combine them with the programming constructs provided to you by the different languages to make an informed decision.
In Brief
The .NET Framework programming model provides the developer with a true choice when it comes to selecting a programming language for a .NET application, Web services, or a component.
C#, Visual Basic, C++, and J# are the four main programming languages for .NET, at least out of the box with the Framework and the Visual Studio .NET tool.
Whereas C# represents the most modern, current, and innovative programming language, Visual Basic .NET stands for high ease of use and developer productivity. C++ provides .NET developers immense power, especially when it comes to lower-level operating systems, Win32 API access, and the most efficient interoperability with existing applications and components. J# provides Java developers with the ease of moving into the .NET programming model.
A number of ISVs have ported various programming languages to output MSIL for the CLR.
Choosing the right programming language primarily depends on your existing skills and the usage. For instance, C++ would be the language of choice for efficient interoperability with the Windows operating system APIs and existing applications.