Public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Instance members are not guaranteed to be
The Expr class is a representation of arbitrary Mathematica expressions in .NET. Exprs are created by reading an expression from a link (using the GetExpr method), they can be decomposed into component Exprs with properties and methods like Head and Part, and their structure can be queried with methods like Length, NumberQ, and VectorQ. All these methods will be familiar to Mathematica programmers, and their Expr counterparts work similarly.
Like Mathematica expressions, Exprs are immutable, meaning they can never be changed once they are created. Operations that might appear to modify an Expr (like Delete) return new modified Exprs without changing the original. Because Exprs are immutable, they are also thread-safe, meaning that any number of threads can access a given Expr at the same time.
Exprs are stored initially in a very efficient way, and they can be created and written to links very quickly. When you call operations that inspect their structure or that extract component parts, however, it is likely that they must be unpacked into a more .NET-native form that requires more memory.
In its present state, Expr has four main uses:
(1) Storing expressions read from a link so that they can be later written to another link. This use replaces functionality that C-language programmers would use a loopback link for. (.NET/Link has an ILoopbackLink interface as well, but Expr affords an even easier method.)
Expr e = ml.GetExpr();
// ... Later, write it to a different MathLink:
Note that if you just want to move an expression immediately from one link to another, you can use the IMathLink method TransferExpression and avoid creating an Expr to store it.
(2) Many of the IKernelLink methods take either a string or an Expr. If it is not convenient to build a string of Mathematica input, you can use an Expr. There are two ways to build an Expr: you can use a constructor, or you can create a loopback link as a scratchpad, build the expression on this link with a series of Put calls, then read the expression off the loopback link using GetExpr. Here is an example that creates an Expr that represents 2+2 and computes it in Mathematica using these two techniques:
// First method: Build it using Expr constructors:
Expr symbolPlus = new Expr(ExpressionType.Symbol, "Plus");
Expr e1 = new Expr(symbolPlus, 2, 2);
// ml is a KernelLink
string result = ml.EvaluateToOutputForm(e1, 0);
// Second method: Build it on an ILoopbackLink with MathLink calls:
ILoopbackLink loop = MathLinkFactory.CreateLoopbackLink();
Expr e2 = loop.GetExpr();
result = ml.EvaluateToOutputForm(e2, 0);
(3) Getting a string representation of an expression. Sometimes you want to be able to produce a readable string form of an entire expression, particularly for debugging. The ToString method will do this for you:
// This code will print out the next expression waiting on the link without
// consuming it, so that the state of the link is unchanged:
Console.WriteLine("Next expression is: " + ml.PeekExpr());
(4) Examining the structure or properties of an expression. Although it is possible to do this sort of thing with MathLink calls, it is very difficult in general. Expr lets you read an entire expression from a link and then examine it using a very high-level interface and without having to worry about managing your current position in an incoming stream of data. Expr has Mathematica-like methods and properties like Head, Part, IntegerQ, VectorQ, and many others to assist in examining an expression.