Introduction

.NET/Link

Welcome to .NET/Link, a product that integrates the Wolfram Language and Microsoft's .NET platform. .NET/Link lets you call .NET from the Wolfram Language in a completely transparent way, and allows you to use and control the Wolfram Language kernel from a .NET program. For Wolfram Language users, .NET/Link makes the entire .NET world an automatic extension to the Wolfram Language environment. For .NET programmers, .NET/Link turns the Wolfram Language into a scripting shell that lets you experiment with, build, and test .NET classes a line at a time. It also makes .NET an ideal environment for writing programs that use the computational services of the Wolfram Language.

.NET/Link's most unique feature is that it lets you load arbitrary .NET types into the Wolfram Language and then create .NET objects and call methods, properties, and so on, directly from the Wolfram Language. Thus, you can use the Wolfram Language to "script" the functionality of an arbitrary .NET programin effect, write a .NET program in the Wolfram Language. Essentially anything you can do from .NET, you can now do from the Wolfram Language, perhaps even more easily because you are working in a true interpreted environment.

.NET/Link also lets you do some very useful things that do not appear to directly involve the .NET runtime. These include calling C-style DLL functions directly from the Wolfram Language, and creating and scripting COM objects, much like Visual Basic can do.

.NET/Link is designed for end-users and developers alike. The same features that let Wolfram Language users transparently call any .NET method also let developers create sophisticated commercial add-ons to the Wolfram Language. Programmers who want to write custom front ends for the Wolfram Language or use the Wolfram Language as a computational engine for another program will find using .NET with .NET/Link is easier than using the traditional Wolfram Symbolic Transfer Protocol (WSTP) interface from C or C++.

.NET/Link is not only for Windows users, as it now runs on Macintosh and Linux as well, although you might need to install the .NET runtime yourself on those systems. The .NET runtime can be obtained from Microsoft at https://dotnet.microsoft.com/en-us/download.

Finally, .NET/Link comes with full source code. You can examine the code to supplement the documentation, get tips for your own programs, better understand how to use advanced features, or just see how it works.

Some familiarity with both .NET and the Wolfram Language is assumed in this tutorial. In Part 2, which covers writing .NET programs that call the Wolfram Language, major examples are generally provided in both C# and Visual Basic .NET versions, although overall the documentation is perhaps slightly more C#-centric. Naturally, when writing .NET programs that use .NET/Link, you can use any .NET-aware language, not just C# and Visual Basic .NET.

"Calling .NET from the Wolfram Language" shows how you use .NET/Link to call .NET from the Wolfram Language and "Calling the Wolfram Language from .NET" shows how to call the Wolfram Language from .NET.

What Is .NET?

.NET is a development platform from Microsoft. It was born on Windows, but it is available for Macintosh and Linux systems as well. The original version of .NET was called the .NET Framework, and has been a standard component of Windows systems for many years. Updates to the .NET Framework ended years ago at version 4.8. Microsoft produced a new version of the .NET runtime called just ".NET" (no "Framework" in the name), which started at version 5 and is up to version 9 at the time of this writing. This is the "modern" version of .NET and most Windows users will already have it installed. You can use either the older .NET Framework or the newer .NET (version 6 and later) with .NET/Link, depending on how you launch it with the InstallNET function.

At the core of .NET is a runtime engine, similar to that used by Java, that loads and executes programs compiled into special bytecodes that the runtime understands. This runtime is called the Common Language Runtime (CLR), but we will often refer to it as the .NET runtime. A key feature of this system is that many languages can be compiled into CLR bytecodes and executed by the runtime. This means that .NET is language neutral, supporting any programming language for which a .NET compiler is available. Microsoft provides compilers for C#, Visual Basic .NET, JScript, F#, and C++ With Managed Extensions. Many other compilers exist, including ones for Fortran, Perl, Python, Eiffel, and COBOL. You can even create a class in one .NET language, say C#, and subclass it in another language.

What Is WSTP?

The Wolfram Symbolic Transfer Protocol (WSTP) is Wolfram Research's protocol for sending data and commands back and forth between the Wolfram Language and other programs. WSTP is the underlying glue that lets .NET and the Wolfram Language talk to each other. When calling .NET from the Wolfram Language, .NET/Link completely hides the low-level details of the WSTP communication, allowing Wolfram Language programmers to load and use .NET classes as if they were part of the Wolfram Language environment itself. When writing .NET programs that call the Wolfram Language, .NET/Link provides a higher-level layer of functionality than the traditional C WSTP programming interface.

How Does .NET/Link Compare to J/Link?

J/Link is an existing Wolfram Research product that integrates Java and the Wolfram Language in almost exactly the same way that .NET/Link integrates .NET and the Wolfram Language. You can use J/Link to do many of the same things you can do with .NET/Link and vice versa. Because it is based on Java, J/Link has the advantage of being cross platform. If you want to write programs that run on every Wolfram Language platform, you should use J/Link. On the other hand, .NET integrates more tightly with the Windows operating system than Java does, so if you want to do Windows-specific things, or you want a very native Windows look and feel, you should use .NET/Link. On Windows, .NET/Link also does some things that J/Link cannot, such as allowing you to call C-style DLL functions directly from the Wolfram Language, or controlling COM objects.

.NET/Link and J/Link provide a very similar programming model. Familiarity with one will be very helpful when working with the other.