This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / Add-ons & Links / .NET/Link / Part 1. Calling .NET from Mathematica: 'Installable .NET' /

Creating Objects

To construct .NET objects, use the NETNew function. The first argument to NETNew is the object's type, specified either as a NETType expression returned from LoadNETType or as a string giving the fully qualified type name (i.e., including the namespace prefix). If you wish to supply any arguments to the object's constructor, they follow as a sequence after the type.

Constructing .NET objects.

For example, this will create a new Form.


The return value from NETNew is a strange expression that looks like it has the head NETObject, except that it is enclosed in angle brackets. The angle brackets are used to indicate that the form in which the expression is displayed is quite different from its internal representation. These expressions will be referred to as NETObject expressions. NETObject expressions are displayed in a way that shows their type name, but you should consider them opaque, meaning that you cannot pick them apart or peer into their insides. You can only use them in .NET/Link functions that take NETObject expressions. For example, if obj is a NETObject, you cannot use First[obj] to get its type name.

NETNew invokes a .NET constructor appropriate for the types of the arguments being passed in, and then returns to Mathematica what is, in effect, a reference to the object. That is how you should think of NETObject expressions—as references to .NET objects very much like object references in a .NET language like C# or Visual Basic .NET. What is returned to Mathematica is not large no matter what type of object you are constructing. In particular, the object's data (that is, its fields) are not sent back to Mathematica. The actual object remains on the .NET side, and Mathematica gets a reference to it.

The previous examples specified the class by giving its name as a string. You can also use a NETType expression, which is a special expression returned by LoadNETType that identifies a class. When you specify the class name as a string, the class is loaded if it has not already been.


NETNew is not the only way to get a reference to a .NET object in Mathematica. Many methods and properties return objects, and when you call such a method or property, a NETObject expression is created. Such objects can be used in the same way as ones you explicitly construct with NETNew.