sitecore Diagnostics: Asserts Argument Checks

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

ArgumentCondition

This method checks to see if the argument specified is true. It also takes in the name of the argument that is logged if the condition fails.

Assert.ArgumentCondition(pageIndex >= 0, "pageIndex", "Value must be greater than or equal to zero.");

ArgumentNotNull

This method ensures that the argument passed is not null. There are two signatures for this method, the first takes in an object and a paramter name and does a simple null check.

Assert.ArgumentNotNull(item, "item");

ArgumentNotNullOrEmpty

This is similar to the ArgumentNotNull method, but will also check to see if the object is empty. There are three variants of this method. The first variant takes in a Sitecore ID and an argument name, and checks to see if the ID is null.

var nullId = new new ID("{00000000-0000-0000-0000-000000000000}");

// Both of these calls will result in an exception
Assert.ArgumentNotNullOrEmpty((ID)null, "null");
Assert.ArgumentNotNullOrEmpty(nullId, nameof(nullId));

The second method adds a check to see if the given string is null or empty.

// Both of these calls will result in an exception
Assert.ArgumentNotNullOrEmpty((string)null, "null");
Assert.ArgumentNotNullOrEmpty("", nameof(emptyString));


Got any sitecore Question?