Skip to content

ref: Use .NET 6.0 ArgumentNullException throw helpers #4982

@Flash0ver

Description

@Flash0ver

Summary

We can replace Argument-Null-Validation patterns with a single method invocation.

Remarks

From

if (param is null)
{
  throw new ArgumentNullException(nameof(param));
}

To

ArgumentNullException.ThrowIfNull(param);

This could increase likeliness of inlining (since the method doesn't throw directly anymore),
and also decrease the Assembly size slightly (due to less user code).

Context

The ArgumentNullException.ThrowIfNull Method was added in .NET 6 (net6.0).

In #4879 we updated Polyfill which adds this method (and related methods like ArgumentException.ThrowIfNullOrEmpty and ArgumentOutOfRangeException.ThrowIfNegativeOrZero and more) to our netstandard2.1, netstandard2.0 and net462 targets, via C# 14 Extension members.

These can be enabled via <PolyArgumentExceptions>true</PolyArgumentExceptions>.
See 3daa9fe

Although this makes our code less verbose and more efficient,
as a drawback, it does increase the Assembly Size of the netstandard2.1, netstandard2.0 and net462 targets,
and with that the size of the entire Sentry NuGet package.
See Assembly size impact

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions