MustHaveLength for ImmutableArray
Context
The .NET library Light.GuardClauses already has several assertions for collections. They often rely on IEnumerable<T> or IEnumerable. However, these assertions would result in ImmutableArray<T> being boxed - we want to avoid that by providing dedicated assertions for this type which avoids boxing. For this issue, we implement the MustHaveLength assertion for ImmutableArray<T>.
Tasks for this issue
Notes
- There are already plenty of other assertions and tests in this library. All overloads are placed in the same file in the production code project. The test projects has top-level folders for different groups of assertions, like
CollectionAssertions, StringAssertions, DateTimeAssertions and so on. Please take a look at them to follow a similar structure and code style.
- This assertion specifically targets
ImmutableArray<T> to avoid boxing that would occur with generic IEnumerable<T> extensions.
- Use the
Length property instead of Count as this is the appropriate property for ImmutableArray<T>.
- The assertion should verify that the
ImmutableArray<T> has exactly the expected length.
- If you have any questions or suggestions, please ask me about them.
MustHaveLength for ImmutableArray
Context
The .NET library Light.GuardClauses already has several assertions for collections. They often rely on
IEnumerable<T>orIEnumerable. However, these assertions would result inImmutableArray<T>being boxed - we want to avoid that by providing dedicated assertions for this type which avoids boxing. For this issue, we implement theMustHaveLengthassertion forImmutableArray<T>.Tasks for this issue
Check.MustHaveLength.csalready exists in the root folder of the project - extend this existing file.MustHaveLengthforImmutableArray<T>. It should be placed in the classCheckwhich is marked aspartial.parameterNameandmessagearguments and throw the default exception. The actual exception is thrown in theThrowclass - use the existingThrow.InvalidCollectionCountmethod which is located inExceptionFactory/Throw.InvalidCollectionCount.cs.Throw.CustomExceptionmethod and pass the delegate, the erroneousImmutableArray<T>instance and the expected length.Lengthproperty ofImmutableArray<T>instead ofCountfor performance and correctness.Should()for assertions).Notes
CollectionAssertions,StringAssertions,DateTimeAssertionsand so on. Please take a look at them to follow a similar structure and code style.ImmutableArray<T>to avoid boxing that would occur with genericIEnumerable<T>extensions.Lengthproperty instead ofCountas this is the appropriate property forImmutableArray<T>.ImmutableArray<T>has exactly the expected length.