uCalc API Version: 2.1.3-preview.2 Released: 6/17/2026

Warning

uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.

DefaultCount = [int]

Property

Product: 

Fast Math Parser

Class: 

uCalcBase

Gets the number of uCalc instances currently on the default instance stack.

Remarks

This static property returns the current depth of the default instance stack. This stack manages which uCalc instance is returned by the static uCalc.GetDefaultInstance() method.

⚙️ How the Default Instance Stack Works

uCalc maintains a LIFO (Last-In, First-Out) stack of default instances. This allows different parts of an application to temporarily set their own uCalc instance as the active default without losing the previous context.

  • Pushing: Calling myInstance.IsDefault(true) pushes myInstance onto the top of the stack, making it the new active default.
  • Popping: An instance is automatically popped from the stack when it is disposed or garbage collected. This makes the previously pushed instance the active default again.
  • Clearing: The uCalc.DefaultClear() method removes all instances from the stack except for the original, root instance.

💡 Practical Applications

This mechanism is particularly useful in modular applications. For example, a plugin or a UI component can create and configure its own uCalc instance and temporarily make it the default while it performs calculations. Once the component is done (or disposed), the application's original default uCalc instance is automatically restored.

⚠️ Important Behavior

The default instance stack is never empty. It is initialized with a single root instance when the application starts. If all instances are cleared via uCalc.DefaultClear(), the stack is reset to this single root instance. Therefore, DefaultCount will always return a value of at least 1.

Examples

Number of uCalc instances on the default uCalc instance list
				
					using uCalcSoftware;

var uc = new uCalc();
Console.WriteLine(uCalc.DefaultCount);

uc.IsDefault = true;
Console.WriteLine(uCalc.DefaultCount);

var ucB = new uCalc();
ucB.IsDefault = true;
Console.WriteLine(uCalc.DefaultCount);

var ucC = new uCalc();
ucC.IsDefault = true;
Console.WriteLine(uCalc.DefaultCount);

uCalc.DefaultClear();
Console.WriteLine(uCalc.DefaultCount);
				
			
1
2
3
4
1
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   cout << uCalc::DefaultCount() << endl;

   uc.IsDefault(true);
   cout << uCalc::DefaultCount() << endl;

   uCalc ucB;
   ucB.IsDefault(true);
   cout << uCalc::DefaultCount() << endl;

   uCalc ucC;
   ucC.IsDefault(true);
   cout << uCalc::DefaultCount() << endl;

   uCalc::DefaultClear();
   cout << uCalc::DefaultCount() << endl;
}
				
			
1
2
3
4
1
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Console.WriteLine(uCalc.DefaultCount)
      
      uc.IsDefault = true
      Console.WriteLine(uCalc.DefaultCount)
      
      Dim ucB As New uCalc()
      ucB.IsDefault = true
      Console.WriteLine(uCalc.DefaultCount)
      
      Dim ucC As New uCalc()
      ucC.IsDefault = true
      Console.WriteLine(uCalc.DefaultCount)
      
      uCalc.DefaultClear()
      Console.WriteLine(uCalc.DefaultCount)
   End Sub
End Module
				
			
1
2
3
4
1
Shows the initial count of default instances upon application startup.
				
					using uCalcSoftware;

var uc = new uCalc();
// By default, a single uCalc instance is always available on the stack.
Console.WriteLine($"Initial default instance count: {uCalc.DefaultCount}");
				
			
Initial default instance count: 1
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // By default, a single uCalc instance is always available on the stack.
   cout << "Initial default instance count: " << uCalc::DefaultCount() << endl;
}
				
			
Initial default instance count: 1
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// By default, a single uCalc instance is always available on the stack.
      Console.WriteLine($"Initial default instance count: {uCalc.DefaultCount}")
   End Sub
End Module
				
			
Initial default instance count: 1
Manage separate parser contexts for different application modules.
				
					using uCalcSoftware;

var uc = new uCalc();
// --- Main Application Context ---
// The implicit 'uc' object is the initial default.
uCalc.DefaultInstance.DefineConstant("PI = 3.14159");
Console.WriteLine($"Initial Count: {uCalc.DefaultCount}");

// --- A Plugin needs a temporary, isolated context ---
using (var moduleCalc = new uCalc()) {
   moduleCalc.IsDefault = true; // Push the new instance onto the stack.
   Console.WriteLine($"Count after module pushes new default: {uCalc.DefaultCount}");

   // The module's context is now active.
   // uCalc::DefaultInstance() would now return 'moduleCalc'.
}

// When 'moduleCalc' goes out of scope, it's destroyed and automatically
// removed from the stack, restoring the previous default.

Console.WriteLine($"Count after module instance is disposed: {uCalc.DefaultCount}");

// Verify the original default instance is active again.
var result = uCalc.DefaultInstance.EvalStr("PI");
Console.WriteLine($"Original context restored. PI = {result}");
				
			
Initial Count: 1
Count after module pushes new default: 2
Count after module instance is disposed: 1
Original context restored. PI = 3.14159
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // --- Main Application Context ---
   // The implicit 'uc' object is the initial default.
   uCalc::DefaultInstance().DefineConstant("PI = 3.14159");
   cout << "Initial Count: " << uCalc::DefaultCount() << endl;

   // --- A Plugin needs a temporary, isolated context ---
   {
      uCalc moduleCalc;
      moduleCalc.Owned(); // Causes moduleCalc to be released when it goes out of scope
      moduleCalc.IsDefault(true); // Push the new instance onto the stack.
      cout << "Count after module pushes new default: " << uCalc::DefaultCount() << endl;

      // The module's context is now active.
      // uCalc::DefaultInstance() would now return 'moduleCalc'.
   }

   // When 'moduleCalc' goes out of scope, it's destroyed and automatically
   // removed from the stack, restoring the previous default.

   cout << "Count after module instance is disposed: " << uCalc::DefaultCount() << endl;

   // Verify the original default instance is active again.
   auto result = uCalc::DefaultInstance().EvalStr("PI");
   cout << "Original context restored. PI = " << result << endl;
}
				
			
Initial Count: 1
Count after module pushes new default: 2
Count after module instance is disposed: 1
Original context restored. PI = 3.14159
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// --- Main Application Context ---
      '// The implicit 'uc' object is the initial default.
      uCalc.DefaultInstance.DefineConstant("PI = 3.14159")
      Console.WriteLine($"Initial Count: {uCalc.DefaultCount}")
      
      '// --- A Plugin needs a temporary, isolated context ---
      Using moduleCalc As New uCalc()
         moduleCalc.IsDefault = true '// Push the new instance onto the stack.
         Console.WriteLine($"Count after module pushes new default: {uCalc.DefaultCount}")
         
         '// The module's context is now active.
         '// uCalc::DefaultInstance() would now return 'moduleCalc'.
      End Using
      
      '// When 'moduleCalc' goes out of scope, it's destroyed and automatically
      '// removed from the stack, restoring the previous default.
      
      Console.WriteLine($"Count after module instance is disposed: {uCalc.DefaultCount}")
      
      '// Verify the original default instance is active again.
      Dim result = uCalc.DefaultInstance.EvalStr("PI")
      Console.WriteLine($"Original context restored. PI = {result}")
   End Sub
End Module
				
			
Initial Count: 1
Count after module pushes new default: 2
Count after module instance is disposed: 1
Original context restored. PI = 3.14159
Internal Test: Verify stack count during creation, stacking, and clearing.
				
					using uCalcSoftware;

var uc = new uCalc();
Console.WriteLine($"Initial: {uCalc.DefaultCount}");

// The implicit 'uc' is the first default. Pushing it again adds to the stack.
uc.IsDefault = true;
Console.WriteLine($"After pushing 'uc' again: {uCalc.DefaultCount}");

var ucB = new uCalc();
ucB.IsDefault = true;
Console.WriteLine($"After pushing ucB: {uCalc.DefaultCount}");

var ucC = new uCalc();
ucC.IsDefault = true;
Console.WriteLine($"After pushing ucC: {uCalc.DefaultCount}");

// Clear all user-defined defaults, leaving only the mandatory root instance.
uCalc.DefaultClear();
Console.WriteLine($"After Clear: {uCalc.DefaultCount}");
				
			
Initial: 1
After pushing 'uc' again: 2
After pushing ucB: 3
After pushing ucC: 4
After Clear: 1
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   cout << "Initial: " << uCalc::DefaultCount() << endl;

   // The implicit 'uc' is the first default. Pushing it again adds to the stack.
   uc.IsDefault(true);
   cout << "After pushing 'uc' again: " << uCalc::DefaultCount() << endl;

   uCalc ucB;
   ucB.IsDefault(true);
   cout << "After pushing ucB: " << uCalc::DefaultCount() << endl;

   uCalc ucC;
   ucC.IsDefault(true);
   cout << "After pushing ucC: " << uCalc::DefaultCount() << endl;

   // Clear all user-defined defaults, leaving only the mandatory root instance.
   uCalc::DefaultClear();
   cout << "After Clear: " << uCalc::DefaultCount() << endl;
}
				
			
Initial: 1
After pushing 'uc' again: 2
After pushing ucB: 3
After pushing ucC: 4
After Clear: 1
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Console.WriteLine($"Initial: {uCalc.DefaultCount}")
      
      '// The implicit 'uc' is the first default. Pushing it again adds to the stack.
      uc.IsDefault = true
      Console.WriteLine($"After pushing 'uc' again: {uCalc.DefaultCount}")
      
      Dim ucB As New uCalc()
      ucB.IsDefault = true
      Console.WriteLine($"After pushing ucB: {uCalc.DefaultCount}")
      
      Dim ucC As New uCalc()
      ucC.IsDefault = true
      Console.WriteLine($"After pushing ucC: {uCalc.DefaultCount}")
      
      '// Clear all user-defined defaults, leaving only the mandatory root instance.
      uCalc.DefaultClear()
      Console.WriteLine($"After Clear: {uCalc.DefaultCount}")
   End Sub
End Module
				
			
Initial: 1
After pushing 'uc' again: 2
After pushing ucB: 3
After pushing ucC: 4
After Clear: 1
Inspecting the stack depth as new defaults are added and cleared
				
					using uCalcSoftware;

var uc = new uCalc();
// Check initial state (Root instance only)
Console.WriteLine(uCalc.DefaultCount);

// Push the current 'uc' instance onto the stack
uc.IsDefault = true;
Console.WriteLine(uCalc.DefaultCount);

// Create a new instance and push it
var ucB = new uCalc();
ucB.IsDefault = true;
Console.WriteLine(uCalc.DefaultCount);

// Create another instance and push it
var ucC = new uCalc();
ucC.IsDefault = true;
Console.WriteLine(uCalc.DefaultCount);

// Clear all custom defaults, reverting to the root instance
uCalc.DefaultClear();
Console.WriteLine(uCalc.DefaultCount);
				
			
1
2
3
4
1
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Check initial state (Root instance only)
   cout << uCalc::DefaultCount() << endl;

   // Push the current 'uc' instance onto the stack
   uc.IsDefault(true);
   cout << uCalc::DefaultCount() << endl;

   // Create a new instance and push it
   uCalc ucB;
   ucB.IsDefault(true);
   cout << uCalc::DefaultCount() << endl;

   // Create another instance and push it
   uCalc ucC;
   ucC.IsDefault(true);
   cout << uCalc::DefaultCount() << endl;

   // Clear all custom defaults, reverting to the root instance
   uCalc::DefaultClear();
   cout << uCalc::DefaultCount() << endl;
}
				
			
1
2
3
4
1
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Check initial state (Root instance only)
      Console.WriteLine(uCalc.DefaultCount)
      
      '// Push the current 'uc' instance onto the stack
      uc.IsDefault = true
      Console.WriteLine(uCalc.DefaultCount)
      
      '// Create a new instance and push it
      Dim ucB As New uCalc()
      ucB.IsDefault = true
      Console.WriteLine(uCalc.DefaultCount)
      
      '// Create another instance and push it
      Dim ucC As New uCalc()
      ucC.IsDefault = true
      Console.WriteLine(uCalc.DefaultCount)
      
      '// Clear all custom defaults, reverting to the root instance
      uCalc.DefaultClear()
      Console.WriteLine(uCalc.DefaultCount)
   End Sub
End Module
				
			
1
2
3
4
1