using uCalcSoftware; var uc = new uCalc(); // This example is mainly meant for C++ where it works with ordinary scalar variables // See the other example where we work with a uCalc array // 1. Use an array so the data lives on the heap as a reference type // Boxing is used for scalar variables; a boxed copy of the scalar disconnected from the uCalc // variable would prevent the example from working as expected. // You technically can get around this with the C# "unsafe" directive, but it's not recommended double[] myHostVar = new double[] { 1234.5 }; // 2. Pin the array var myHostVarHandle = System.Runtime.InteropServices.GCHandle.Alloc(myHostVar, System.Runtime.InteropServices.GCHandleType.Pinned); // 3. Bind the uCalc variable to the pinned memory address uc.DefineVariable("myVar", myHostVarHandle.AddrOfPinnedObject()); Console.WriteLine(uc.Eval("myVar")); // Output: 1234.5 // Changing the uCalc variable updates the pinned array uc.Eval("myVar = 456"); // The change is reflected in the C# array Console.WriteLine(myHostVar[0]); // Output: 456 // Changing the C# array updates uCalc myHostVar[0] = 9876; Console.WriteLine(uc.Eval("myVar")); // Output: 9876 // Fast Parse & Evaluate loop var expr = uc.Parse("myVar * 10"); for (myHostVar[0] = 1; myHostVar[0] <= 5; myHostVar[0]++) { Console.WriteLine(expr.Evaluate()); } // Don't forget to free the handle when you are completely done with the parser instance myHostVarHandle.Free();