Skip to main content

Modern C# in Acumatica: Refactoring for Optimal Code Clarity and Performance. Part 2.

  • November 27, 2025
  • 0 replies
  • 23 views

Forum|alt.badge.img+1

How modern C# features transform legacy Acumatica code into elegant and efficient solutions through optimization

This content was prepared by a human using a text editor, Visual Studio 2026, AI tools, and materials from open Internet sources.

 

In Part 2, we'll explore the following C# language features:

  1. Expression-bodied members — shortening methods, properties, constructors;
  2. Target-typed new — new() instead of new Type();
  3. Throw-expression — throw in expressions;
  4. Using declaration — using var x = ... without a { } block;
  5. Static local functions — private functions inside methods without context capture.

Let's dive in.

 

6. Expression-bodied members

C# / .NET Version

  • C# 6.0 (improvements in 7.0 and 8.0)
  • Works in .NET Framework 4.8

Purpose

  • Shortening trivial methods, properties, and constructors
  • Reducing boilerplate
  • One-line members become more readable

C# Example

/// <summary>
/// Expression-bodied method example that returns whether an item is shippable.
/// </summary>
private bool IsShippable(int openQty) => openQty > 0;

Acumatica Example (optimized)

private bool IsShippable(SOOrder o) => o?.Hold == false && o.OpenOrderQty > 0;

Acumatica Example (not optimized)

private bool IsShippable(int openQty)
{
return openQty > 0;
}

7. Target-typed new

C# / .NET Version

  • C# 9.0
  • Works in .NET Framework 4.8 when using the Roslyn compiler (Acumatica supports this).

Purpose

  • Reduces type duplication
  • Enables writing new() instead of new Type() where the type is already known from context
  • Makes collection and DTO declarations shorter and cleaner

C# Example

/// <summary>
/// Uses target-typed new to create and return a List of strings without repeating the type.
/// </summary>
private List<string> CreateList() => new();

Acumatica Example (optimized)

private List<string> OrderTypes() => new();

Acumatica Example (not optimized)

private List<string> OrderTypes() => new List<string>();

8. Throw-expression

C# / .NET Version

  • C# 7.0
  • Fully works in .NET Framework 4.8

Purpose

  • Allows using throw in expressions
  • Reduces boilerplate in argument checks
  • Makes guard expressions compact
  • Especially convenient in property initialization, ternary operators, lambdas

C# Example

/// <summary>
/// Demonstrates the throw-expression to throw an exception inline when a value is null.
/// </summary>
private void EnsureNotNull(object o) => _ = o ?? throw new Exception("Value required!");

Acumatica Example (optimized)

private SOOrder Ensure(SOOrder o) =>
o ?? throw new PXException(Messages.OrderIsRequired);

Acumatica Example (not optimized)

private void EnsureNotNull(object o)
{
if (o == null)
throw new Exception("Value required!");
}

9. Using declaration

C# / .NET Version

  • C# 8.0
  • Works in .NET Framework 4.8

Purpose

  • Eliminates unnecessary nesting levels
  • Allows declaring using without a { } block
  • Resource is disposed automatically at the end of the scope

C# Example

/// Demonstrates a using declaration with <see cref="HttpClient"/> and an async call.
/// The <see cref="HttpClient"/> is disposed automatically at the end of the method.
/// </summary>
private async System.Threading.Tasks.Task LogHttp()
{
using HttpClient client = new();
var s = await client.GetStringAsync("https://example.com");
Console.WriteLine(s.Substring(0, Math.Min(30, s.Length)) + "...");
}

Acumatica Example (optimized)

public void LogHttp()
{
using HttpClient client = new();
var result = client.GetStringAsync("https://example.com").Result;
PXTrace.WriteInformation(result);
}

Acumatica Example (not optimized)

private async System.Threading.Tasks.Task LogHttp()
{
using (HttpClient client = new HttpClient())
{
var s = await client.GetStringAsync("https://example.com");
PXTrace.WriteInformation(s.Substring(0, Math.Min(30, s.Length)) + "...");
}
}

10. Static local functions

C# / .NET Version

  • C# 8.0
  • Works in .NET Framework 4.8

Purpose

  • Reduces accidental context captures
  • Improves performance of inner functions
  • Makes side effects explicit

C# Example

/// Demonstrates a static local function used to encapsulate small validation logic.
/// The local static function cannot capture outer variables and is declared with 'static'.
/// </summary>
private bool Validate(string s)
{
return IsBad(s);

static bool IsBad(string val) => val.StartsWith("ERR");
}

Acumatica Example (optimized)

private bool ValidateOrderNbr(string value)
{
return IsBad(value);

static bool IsBad(string s) =>
s?.StartsWith("ERR", StringComparison.OrdinalIgnoreCase) == true;
}

Acumatica Example (not optimized)

private bool Validate(string s)
{
return ValidateInternal(s);
}

private static bool ValidateInternal(string val)
{
return val.StartsWith("ERR");
}

End of Part 2.