diff --git a/Code/Light.GuardClauses.AllProjects.sln.DotSettings b/Code/Light.GuardClauses.AllProjects.sln.DotSettings
index 2c4519b..aceafc4 100644
--- a/Code/Light.GuardClauses.AllProjects.sln.DotSettings
+++ b/Code/Light.GuardClauses.AllProjects.sln.DotSettings
@@ -2,16 +2,32 @@
True
None
False
+ WARNING
+ WARNING
+ DO_NOT_SHOW
HINT
HINT
+ WARNING
HINT
WARNING
+ True
<?xml version="1.0" encoding="utf-16"?><Profile name="Kenny's Kleanup"><CSReorderTypeMembers>True</CSReorderTypeMembers><CSUpdateFileHeader>True</CSUpdateFileHeader><CSEnforceVarKeywordUsageSettings>True</CSEnforceVarKeywordUsageSettings><CSMakeFieldReadonly>True</CSMakeFieldReadonly><CSArrangeQualifiers>True</CSArrangeQualifiers><CSOptimizeUsings><OptimizeUsings>True</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><CSShortenReferences>True</CSShortenReferences><CSReformatCode>True</CSReformatCode><CSharpFormatDocComments>True</CSharpFormatDocComments><CppReformatCode>True</CppReformatCode><XMLReformatCode>True</XMLReformatCode><CssReformatCode>True</CssReformatCode><CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="False" AddMissingParentheses="False" ArrangeBraces="False" ArrangeAttributes="False" ArrangeArgumentsStyle="False" ArrangeCodeBodyStyle="False" ArrangeVarStyle="False" /><CSMakeAutoPropertyGetOnly>True</CSMakeAutoPropertyGetOnly><RemoveCodeRedundancies>True</RemoveCodeRedundancies><HtmlReformatCode>True</HtmlReformatCode><JsInsertSemicolon>True</JsInsertSemicolon><CorrectVariableKindsDescriptor>True</CorrectVariableKindsDescriptor><JsReformatCode>True</JsReformatCode><JsFormatDocComments>True</JsFormatDocComments><FormatAttributeQuoteDescriptor>True</FormatAttributeQuoteDescriptor></Profile>
- C# Cleanup
- Kenny's Kleanup
+ Kenny's Kleanup
+ Built-in: Full Cleanup
+ False
+ Required
+ Required
+ Required
+ Required
+ ExpressionBody
+ ExpressionBody
+ ExpressionBody
+ True
+ False
True
True
+ True
True
@@ -22,23 +38,64 @@
True
True
True
+ True
TOGETHER_SAME_LINE
+ NO_INDENT
True
True
+ True
+ True
+ False
True
+ True
+ False
+ True
+ 1
+ 3
+ COMPACT
True
+ True
True
True
NEVER
- NEVER
- NEVER
- NEVER
+
+
+
+ NEVER
+ False
+ ALWAYS
+ IF_OWNER_IS_SINGLE_LINE
+ NEVER
DO_NOT_CHANGE
+ True
+ True
True
- 500
- False
+ False
+ True
+ True
+ CHOP_IF_LONG
+ CHOP_IF_LONG
+ False
+ True
+ True
+ True
+ True
+ True
+ False
+ CHOP_IF_LONG
+ CHOP_IF_LONG
+ CHOP_IF_LONG
+
+
+ CHOP_IF_LONG
+ CHOP_ALWAYS
+ CHOP_IF_LONG
RemoveIndent
RemoveIndent
+ False
+ ByFirstAttr
+ 150
+ False
False
False
False
@@ -192,16 +249,21 @@
</TypePattern>
</Patterns>
False
+ False
False
True
Replace
Replace
False
+ True
True
False
False
True
False
+ False
+ True
+ False
<Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
<Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
<Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" />
@@ -256,12 +318,4 @@
True
True
True
- True
- True
- True
- True
- True
- True
- True
- True
- True
\ No newline at end of file
+ True
\ No newline at end of file
diff --git a/Code/Light.GuardClauses.sln.DotSettings b/Code/Light.GuardClauses.sln.DotSettings
index 47d8c24..aceafc4 100644
--- a/Code/Light.GuardClauses.sln.DotSettings
+++ b/Code/Light.GuardClauses.sln.DotSettings
@@ -2,16 +2,32 @@
True
None
False
+ WARNING
+ WARNING
+ DO_NOT_SHOW
HINT
HINT
+ WARNING
HINT
WARNING
+ True
<?xml version="1.0" encoding="utf-16"?><Profile name="Kenny's Kleanup"><CSReorderTypeMembers>True</CSReorderTypeMembers><CSUpdateFileHeader>True</CSUpdateFileHeader><CSEnforceVarKeywordUsageSettings>True</CSEnforceVarKeywordUsageSettings><CSMakeFieldReadonly>True</CSMakeFieldReadonly><CSArrangeQualifiers>True</CSArrangeQualifiers><CSOptimizeUsings><OptimizeUsings>True</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><CSShortenReferences>True</CSShortenReferences><CSReformatCode>True</CSReformatCode><CSharpFormatDocComments>True</CSharpFormatDocComments><CppReformatCode>True</CppReformatCode><XMLReformatCode>True</XMLReformatCode><CssReformatCode>True</CssReformatCode><CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="False" AddMissingParentheses="False" ArrangeBraces="False" ArrangeAttributes="False" ArrangeArgumentsStyle="False" ArrangeCodeBodyStyle="False" ArrangeVarStyle="False" /><CSMakeAutoPropertyGetOnly>True</CSMakeAutoPropertyGetOnly><RemoveCodeRedundancies>True</RemoveCodeRedundancies><HtmlReformatCode>True</HtmlReformatCode><JsInsertSemicolon>True</JsInsertSemicolon><CorrectVariableKindsDescriptor>True</CorrectVariableKindsDescriptor><JsReformatCode>True</JsReformatCode><JsFormatDocComments>True</JsFormatDocComments><FormatAttributeQuoteDescriptor>True</FormatAttributeQuoteDescriptor></Profile>
- C# Cleanup
- Kenny's Kleanup
+ Kenny's Kleanup
+ Built-in: Full Cleanup
+ False
+ Required
+ Required
+ Required
+ Required
+ ExpressionBody
+ ExpressionBody
+ ExpressionBody
+ True
+ False
True
True
+ True
True
@@ -22,23 +38,64 @@
True
True
True
+ True
TOGETHER_SAME_LINE
+ NO_INDENT
True
True
+ True
+ True
+ False
True
+ True
+ False
+ True
+ 1
+ 3
+ COMPACT
True
+ True
True
True
NEVER
- NEVER
- NEVER
- NEVER
+
+
+
+ NEVER
+ False
+ ALWAYS
+ IF_OWNER_IS_SINGLE_LINE
+ NEVER
DO_NOT_CHANGE
+ True
+ True
True
- 500
- False
+ False
+ True
+ True
+ CHOP_IF_LONG
+ CHOP_IF_LONG
+ False
+ True
+ True
+ True
+ True
+ True
+ False
+ CHOP_IF_LONG
+ CHOP_IF_LONG
+ CHOP_IF_LONG
+
+
+ CHOP_IF_LONG
+ CHOP_ALWAYS
+ CHOP_IF_LONG
RemoveIndent
RemoveIndent
+ False
+ ByFirstAttr
+ 150
+ False
False
False
False
@@ -192,16 +249,21 @@
</TypePattern>
</Patterns>
False
+ False
False
True
Replace
Replace
False
+ True
True
False
False
True
False
+ False
+ True
+ False
<Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
<Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
<Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" />
@@ -255,4 +317,5 @@
True
True
True
- True
\ No newline at end of file
+ True
+ True
\ No newline at end of file
diff --git a/Code/Light.GuardClauses/CallerArgumentExpressionAttribute.cs b/Code/Light.GuardClauses/CallerArgumentExpressionAttribute.cs
index 36a1f72..f5df610 100644
--- a/Code/Light.GuardClauses/CallerArgumentExpressionAttribute.cs
+++ b/Code/Light.GuardClauses/CallerArgumentExpressionAttribute.cs
@@ -2,14 +2,12 @@
// ReSharper disable once CheckNamespace -- CallerArgumentExpression must be in exactly this namespace
namespace System.Runtime.CompilerServices;
-[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
+[AttributeUsage(AttributeTargets.Parameter)]
internal sealed class CallerArgumentExpressionAttribute : Attribute
{
- public CallerArgumentExpressionAttribute(string parameterName)
- {
+ public CallerArgumentExpressionAttribute(string parameterName) =>
ParameterName = parameterName;
- }
public string ParameterName { get; }
}
-#endif
\ No newline at end of file
+#endif
diff --git a/Code/Light.GuardClauses/Check.CommonAssertions.cs b/Code/Light.GuardClauses/Check.CommonAssertions.cs
deleted file mode 100644
index 0104f58..0000000
--- a/Code/Light.GuardClauses/Check.CommonAssertions.cs
+++ /dev/null
@@ -1,714 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.CompilerServices;
-using JetBrains.Annotations;
-using Light.GuardClauses.Exceptions;
-using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
-
-namespace Light.GuardClauses;
-
-///
-/// The class provides access to all assertions of Light.GuardClauses.
-///
-public static partial class Check
-{
- ///
- /// Ensures that the specified object reference is not null, or otherwise throws an .
- ///
- /// The object reference to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustNotBeNull([NotNull, ValidatedNotNull, NoEnumeration] this T? parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : class
- {
- if (parameter is null)
- Throw.ArgumentNull(parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified object reference is not null, or otherwise throws your custom exception.
- ///
- /// The reference to be checked.
- /// The delegate that creates your custom exception.
- /// Your custom exception thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustNotBeNull([NotNull, ValidatedNotNull, NoEnumeration] this T? parameter, Func exceptionFactory) where T : class
- {
- if (parameter is null)
- Throw.CustomException(exceptionFactory);
- return parameter;
- }
-
- ///
- /// Ensures that the specified parameter is not the default value, or otherwise throws an
- /// for reference types, or an for value types.
- ///
- /// The value to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is a reference type and null.
- /// Thrown when is a value type and the default value.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustNotBeDefault([NotNull, ValidatedNotNull] this T parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (default(T) is null)
- {
- if (parameter is null)
- Throw.ArgumentNull(parameterName, message);
- return parameter;
- }
-
- if (EqualityComparer.Default.Equals(parameter, default!))
- Throw.ArgumentDefault(parameterName, message);
-
- // If we end up here, we have a value type which cannot be null
-#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
- return parameter;
-#pragma warning restore CS8777
- }
-
- ///
- /// Ensures that the specified parameter is not the default value, or otherwise throws your custom exception.
- ///
- /// The value to be checked.
- /// The delegate that creates your custom exception.
- /// Your custom exception thrown when is the default value.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustNotBeDefault([NotNull, ValidatedNotNull] this T parameter, Func exceptionFactory)
- {
- if (default(T) is null)
- {
- if (parameter is null)
- Throw.CustomException(exceptionFactory);
- return parameter;
- }
-
- if (EqualityComparer.Default.Equals(parameter, default!))
- Throw.CustomException(exceptionFactory);
-
- // If we end up here, we have a value type which cannot be null
-#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
- return parameter;
-#pragma warning restore CS8777
-
- }
-
- ///
- /// Ensures that the specified parameter is not null when is a reference type, or otherwise
- /// throws an . PLEASE NOTICE: you should only use this assertion in generic contexts,
- /// use by default.
- ///
- /// The value to be checked for null.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is a reference type and is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustNotBeNullReference([NotNull, ValidatedNotNull, NoEnumeration] this T parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (default(T) != null)
- {
- // If we end up here, parameter cannot be null
-#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
- return parameter;
-#pragma warning restore CS8777 // Parameter must have a non-null value when exiting.
- }
-
- if (parameter is null)
- Throw.ArgumentNull(parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified parameter is not null when is a reference type, or otherwise
- /// throws your custom exception. PLEASE NOTICE: you should only use this assertion in generic contexts,
- /// use by default.
- ///
- /// The value to be checked for null.
- /// The delegate that creates your custom exception.
- /// Your custom exception thrown when is a reference type and is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustNotBeNullReference([NotNull, ValidatedNotNull, NoEnumeration] this T parameter, Func exceptionFactory)
- {
- if (default(T) != null)
- {
- // If we end up here, parameter cannot be null
-#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
- return parameter;
-#pragma warning restore CS8777 // Parameter must have a non-null value when exiting.
- }
-
- if (parameter is null)
- Throw.CustomException(exceptionFactory);
- return parameter;
- }
-
- ///
- /// Ensures that can be cast to and returns the cast value, or otherwise throws a .
- ///
- /// The value to be cast.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when cannot be cast to .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustBeOfType([NotNull, ValidatedNotNull, NoEnumeration] this object? parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.MustNotBeNull(parameterName, message) is T castValue)
- return castValue;
-
- Throw.InvalidTypeCast(parameter, typeof(T), parameterName, message);
- return default;
- }
-
- ///
- /// Ensures that can be cast to and returns the cast value, or otherwise throws your custom exception.
- ///
- /// The value to be cast.
- /// The delegate that creates your custom exception. The is passed to this delegate.
- /// Your custom exception thrown when cannot be cast to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustBeOfType([NotNull, ValidatedNotNull, NoEnumeration] this object? parameter, Func exceptionFactory)
- {
- if (parameter is T castValue)
- return castValue;
-
- Throw.CustomException(exceptionFactory, parameter);
- return default;
- }
-
- ///
- /// Checks if the specified value is a valid enum value of its type. This is true when the specified value
- /// is one of the constants defined in the enum, or a valid flags combination when the enum type is marked
- /// with the .
- ///
- /// The type of the enum.
- /// The enum value to be checked.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsValidEnumValue(this T parameter) where T : struct, Enum
- => EnumInfo.IsValidEnumValue(parameter);
-
- ///
- /// Ensures that the specified enum value is valid, or otherwise throws an . An enum value
- /// is valid when the specified value is one of the constants defined in the enum, or a valid flags combination when the enum type
- /// is marked with the .
- ///
- /// The type of the enum.
- /// The value to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is no valid enum value.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static T MustBeValidEnumValue(this T parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : struct, Enum
- {
- if (!EnumInfo.IsValidEnumValue(parameter))
- Throw.EnumValueNotDefined(parameter, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified enum value is valid, or otherwise throws your custom exception. An enum value
- /// is valid when the specified value is one of the constants defined in the enum, or a valid flags combination when the enum type
- /// is marked with the .
- ///
- /// The type of the enum.
- /// The value to be checked.
- /// The delegate that creates your custom exception. The is passed to this delegate.
- /// Your custom exception thrown when is no valid enum value, or when is no enum type.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("exceptionFactory:null => halt")]
- public static T MustBeValidEnumValue(this T parameter, Func exceptionFactory) where T : struct, Enum
- {
- if (!EnumInfo.IsValidEnumValue(parameter))
- Throw.CustomException(exceptionFactory, parameter);
- return parameter;
- }
-
- ///
- /// Checks if the specified GUID is an empty one.
- ///
- /// The GUID to be checked.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsEmpty(this Guid parameter) => parameter == Guid.Empty;
-
- ///
- /// Ensures that the specified GUID is not empty, or otherwise throws an .
- ///
- /// The GUID to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is an empty GUID.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Guid MustNotBeEmpty(this Guid parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter == Guid.Empty)
- Throw.EmptyGuid(parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified GUID is not empty, or otherwise throws your custom exception.
- ///
- /// The GUID to be checked.
- /// The delegate that creates your custom exception.
- /// Your custom exception thrown when is an empty GUID.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("exceptionFactory:null => halt")]
- public static Guid MustNotBeEmpty(this Guid parameter, Func exceptionFactory)
- {
- if (parameter == Guid.Empty)
- Throw.CustomException(exceptionFactory);
- return parameter;
- }
-
- ///
- /// Checks if the specified is true and throws an in this case.
- ///
- /// The condition to be checked. The exception is thrown when it is true.
- /// The message that will be passed to the (optional).
- /// Thrown when is true.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void InvalidOperation(bool condition, string? message = null)
- {
- if (condition)
- Throw.InvalidOperation(message);
- }
-
- ///
- /// Checks if the specified is true and throws an in this case.
- ///
- /// The condition to be checked. The exception is thrown when it is true.
- /// The message that will be passed to the .
- /// Thrown when is true.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void InvalidState(bool condition, string? message = null)
- {
- if (condition)
- Throw.InvalidState(message);
- }
-
- ///
- /// Checks if the specified is true and throws an in this case.
- ///
- /// The condition to be checked. The exception is thrown when it is true.
- /// The name of the parameter (optional).
- /// The message that will be passed to the (optional).
- /// Thrown when is true.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void InvalidArgument(bool condition, string? parameterName = null, string? message = null)
- {
- if (condition)
- Throw.Argument(parameterName, message);
- }
-
- ///
- /// Checks if the specified is true and throws your custom exception in this case.
- ///
- /// The condition to be checked. The exception is thrown when it is true.
- /// The delegate that creates your custom exception.
- /// Your custom exception thrown when is true.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("exceptionFactory:null => halt")]
- public static void InvalidArgument(bool condition, Func exceptionFactory)
- {
- if (condition)
- Throw.CustomException(exceptionFactory);
- }
-
- ///
- /// Checks if the specified is true and throws your custom exception in this case.
- ///
- /// The condition to be checked. The exception is thrown when it is true.
- /// The value that is checked in the . This value is passed to the .
- /// The delegate that creates your custom exception. The is passed to this delegate.
- /// Your custom exception thrown when is true.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("exceptionFactory:null => halt")]
- public static void InvalidArgument(bool condition, T parameter, Func exceptionFactory)
- {
- if (condition)
- Throw.CustomException(exceptionFactory, parameter);
- }
-
- ///
- /// Ensures that the specified nullable has a value and returns it, or otherwise throws a .
- ///
- /// The nullable to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when has no value.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static T MustHaveValue([NotNull, NoEnumeration] this T? parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : struct
- {
- if (!parameter.HasValue)
- Throw.NullableHasNoValue(parameterName, message);
-
- return parameter.Value;
- }
-
- ///
- /// Ensures that the specified nullable has a value and returns it, or otherwise throws your custom exception.
- ///
- /// The nullable to be checked.
- /// The delegate that creates your custom exception.
- /// Thrown when has no value.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("exceptionFactory:null => halt")]
- public static T MustHaveValue([NotNull, NoEnumeration] this T? parameter, Func exceptionFactory) where T : struct
- {
- if (!parameter.HasValue)
- Throw.CustomException(exceptionFactory);
-
- return parameter.Value;
- }
-
- ///
- /// Checks if and point to the same object.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- // ReSharper disable StringLiteralTypo
- [ContractAnnotation("parameter:notNull => true, other:notnull; parameter:notNull => false, other:canbenull; other:notnull => true, parameter:notnull; other:notnull => false, parameter:canbenull")]
- // ReSharper restore StringLiteralTypo
- public static bool IsSameAs([NoEnumeration] this T? parameter, [NoEnumeration] T? other) where T : class =>
- ReferenceEquals(parameter, other);
-
- ///
- /// Ensures that and do not point to the same object instance, or otherwise
- /// throws a .
- ///
- /// The first reference to be checked.
- /// The second reference to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when both and point to the same object.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static T? MustNotBeSameAs([NoEnumeration] this T? parameter, [NoEnumeration] T? other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : class
- {
- if (ReferenceEquals(parameter, other))
- Throw.SameObjectReference(parameter, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that and do not point to the same object instance, or otherwise
- /// throws your custom exception.
- ///
- /// The first reference to be checked.
- /// The second reference to be checked.
- /// The delegate that creates your custom exception. is passed to this delegate.
- /// Thrown when both and point to the same object.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static T? MustNotBeSameAs([NoEnumeration] this T? parameter, T? other, Func exceptionFactory) where T : class
- {
- if (ReferenceEquals(parameter, other))
- Throw.CustomException(exceptionFactory, parameter);
- return parameter;
- }
-
- ///
- /// Ensures that is equal to using the default equality comparer, or otherwise throws a .
- ///
- /// The first value to be compared.
- /// The other value to be compared.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when and are not equal.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static T MustBe(this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (!EqualityComparer.Default.Equals(parameter, other))
- Throw.ValuesNotEqual(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that is equal to using the default equality comparer, or otherwise throws your custom exception.
- ///
- /// The first value to be compared.
- /// The other value to be compared.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when and are not equal.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static T MustBe(this T parameter, T other, Func exceptionFactory)
- {
- if (!EqualityComparer.Default.Equals(parameter, other))
- Throw.CustomException(exceptionFactory, parameter, other);
- return parameter;
- }
-
- ///
- /// Ensures that is equal to using the specified equality comparer, or otherwise throws a .
- ///
- /// The first value to be compared.
- /// The other value to be compared.
- /// The equality comparer used for comparing the two values.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when and are not equal.
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("equalityComparer:null => halt")]
- public static T MustBe(this T parameter, T other, IEqualityComparer equalityComparer, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (!equalityComparer.MustNotBeNull(nameof(equalityComparer), message).Equals(parameter, other))
- Throw.ValuesNotEqual(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that is equal to using the specified equality comparer, or otherwise throws your custom exception.
- ///
- /// The first value to be compared.
- /// The other value to be compared.
- /// The equality comparer used for comparing the two values.
- /// The delegate that creates your custom exception. , , and are passed to this delegate.
- /// Your custom exception thrown when and are not equal, or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("equalityComparer:null => halt")]
- public static T MustBe(this T parameter, T other, IEqualityComparer equalityComparer, Func, Exception> exceptionFactory)
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (equalityComparer is null || !equalityComparer.Equals(parameter, other))
- Throw.CustomException(exceptionFactory, parameter, other, equalityComparer!);
- return parameter;
- }
-
- ///
- /// Ensures that is not equal to using the default equality comparer, or otherwise throws a .
- ///
- /// The first value to be compared.
- /// The other value to be compared.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when and are equal.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static T MustNotBe(this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (EqualityComparer.Default.Equals(parameter, other))
- Throw.ValuesEqual(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that is not equal to using the default equality comparer, or otherwise throws your custom exception.
- ///
- /// The first value to be compared.
- /// The other value to be compared.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when and are equal.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static T MustNotBe(this T parameter, T other, Func exceptionFactory)
- {
- if (EqualityComparer.Default.Equals(parameter, other))
- Throw.CustomException(exceptionFactory, parameter, other);
- return parameter;
- }
-
- ///
- /// Ensures that is not equal to using the specified equality comparer, or otherwise throws a .
- ///
- /// The first value to be compared.
- /// The other value to be compared.
- /// The equality comparer used for comparing the two values.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when and are equal.
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("equalityComparer:null => halt")]
- public static T MustNotBe(this T parameter, T other, IEqualityComparer equalityComparer, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (equalityComparer.MustNotBeNull(nameof(equalityComparer), message).Equals(parameter, other))
- Throw.ValuesEqual(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that is not equal to using the specified equality comparer, or otherwise throws your custom exception.
- ///
- /// The first value to be compared.
- /// The other value to be compared.
- /// The equality comparer used for comparing the two values.
- /// The delegate that creates your custom exception. , , and are passed to this delegate.
- /// Your custom exception thrown when and are equal, or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("equalityComparer:null => halt")]
- public static T MustNotBe(this T parameter, T other, IEqualityComparer equalityComparer, Func, Exception> exceptionFactory)
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (equalityComparer is null || equalityComparer.Equals(parameter, other))
- Throw.CustomException(exceptionFactory, parameter, other, equalityComparer!);
- return parameter;
- }
-
- ///
- /// Checks if the specified value is approximately the same as the other value, using the given tolerance.
- ///
- /// The first value to be compared.
- /// The second value to be compared.
- /// The tolerance indicating how much the two values may differ from each other.
- ///
- /// True if and are equal or if their absolute difference
- /// is smaller than the given , otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsApproximately(this double value, double other, double tolerance) =>
- Math.Abs(value - other) < tolerance;
-
- ///
- /// Checks if the specified value is approximately the same as the other value, using the default tolerance of 0.0001.
- ///
- /// The first value to be compared.
- /// The second value to be compared.
- ///
- /// True if and are equal or if their absolute difference
- /// is smaller than 0.0001, otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsApproximately(this double value, double other) =>
- Math.Abs(value - other) < 0.0001;
-
- ///
- /// Checks if the specified value is approximately the same as the other value, using the given tolerance.
- ///
- /// The first value to compare.
- /// The second value to compare.
- /// The tolerance indicating how much the two values may differ from each other.
- ///
- /// True if and are equal or if their absolute difference
- /// is smaller than the given , otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsApproximately(this float value, float other, float tolerance) =>
- Math.Abs(value - other) < tolerance;
-
- ///
- /// Checks if the specified value is approximately the same as the other value, using the default tolerance of 0.0001f.
- ///
- /// The first value to be compared.
- /// The second value to be compared.
- ///
- /// True if and are equal or if their absolute difference
- /// is smaller than 0.0001f, otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsApproximately(this float value, float other) =>
- Math.Abs(value - other) < 0.0001f;
-
- ///
- /// Checks if the specified value is greater than or approximately the same as the other value, using the given tolerance.
- ///
- /// The first value to compare.
- /// The second value to compare.
- /// The tolerance indicating how much the two values may differ from each other.
- ///
- /// True if is greater than or if their absolute difference
- /// is smaller than the given , otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsGreaterThanOrApproximately(this double value, double other, double tolerance) =>
- value > other || value.IsApproximately(other, tolerance);
-
- ///
- /// Checks if the specified value is greater than or approximately the same as the other value, using the default tolerance of 0.0001.
- ///
- /// The first value to compare.
- /// The second value to compare.
- ///
- /// True if is greater than or if their absolute difference
- /// is smaller than 0.0001, otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsGreaterThanOrApproximately(this double value, double other) =>
- value > other || value.IsApproximately(other);
-
- ///
- /// Checks if the specified value is greater than or approximately the same as the other value, using the given tolerance.
- ///
- /// The first value to compare.
- /// The second value to compare.
- /// The tolerance indicating how much the two values may differ from each other.
- ///
- /// True if is greater than or if their absolute difference
- /// is smaller than the given , otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsGreaterThanOrApproximately(this float value, float other, float tolerance) =>
- value > other || value.IsApproximately(other, tolerance);
-
- ///
- /// Checks if the specified value is greater than or approximately the same as the other value, using the default tolerance of 0.0001f.
- ///
- /// The first value to compare.
- /// The second value to compare.
- ///
- /// True if is greater than or if their absolute difference
- /// is smaller than 0.0001, otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsGreaterThanOrApproximately(this float value, float other) =>
- value > other || value.IsApproximately(other);
-
- ///
- /// Checks if the specified value is less than or approximately the same as the other value, using the given tolerance.
- ///
- /// The first value to compare.
- /// The second value to compare.
- /// The tolerance indicating how much the two values may differ from each other.
- ///
- /// True if is less than or if their absolute difference
- /// is smaller than the given , otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsLessThanOrApproximately(this double value, double other, double tolerance) =>
- value < other || value.IsApproximately(other, tolerance);
-
- ///
- /// Checks if the specified value is less than or approximately the same as the other value, using the default tolerance of 0.0001.
- ///
- /// The first value to compare.
- /// The second value to compare.
- ///
- /// True if is less than or if their absolute difference
- /// is smaller than 0.0001, otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsLessThanOrApproximately(this double value, double other) =>
- value < other || value.IsApproximately(other);
-
- ///
- /// Checks if the specified value is less than or approximately the same as the other value, using the given tolerance.
- ///
- /// The first value to compare.
- /// The second value to compare.
- /// The tolerance indicating how much the two values may differ from each other.
- ///
- /// True if is less than or if their absolute difference
- /// is smaller than the given , otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsLessThanOrApproximately(this float value, float other, float tolerance) =>
- value < other || value.IsApproximately(other, tolerance);
-
- ///
- /// Checks if the specified value is less than or approximately the same as the other value, using the default tolerance of 0.0001f.
- ///
- /// The first value to compare.
- /// The second value to compare.
- ///
- /// True if is less than or if their absolute difference
- /// is smaller than 0.0001f, otherwise false.
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsLessThanOrApproximately(this float value, float other) =>
- value < other || value.IsApproximately(other);
-}
\ No newline at end of file
diff --git a/Code/Light.GuardClauses/Check.ComparableAssertions.cs b/Code/Light.GuardClauses/Check.ComparableAssertions.cs
deleted file mode 100644
index 0eb9020..0000000
--- a/Code/Light.GuardClauses/Check.ComparableAssertions.cs
+++ /dev/null
@@ -1,411 +0,0 @@
-using System;
-using JetBrains.Annotations;
-using System.Runtime.CompilerServices;
-using Light.GuardClauses.Exceptions;
-using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
-
-namespace Light.GuardClauses;
-
-public static partial class Check
-{
- /*
- * -------------------------------------
- * Must Not Be Less Than
- * Must Be Greater Than or Equal To
- * -------------------------------------
- */
- ///
- /// Ensures that the specified is not less than the given value, or otherwise throws an .
- ///
- /// The comparable to be checked.
- /// The boundary value that must be less than or equal to .
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when the specified is less than .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustNotBeLessThan([NotNull, ValidatedNotNull] this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (parameter.MustNotBeNullReference(parameterName, message).CompareTo(other) < 0)
- Throw.MustNotBeLessThan(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is not less than the given value, or otherwise throws your custom exception.
- ///
- /// The comparable to be checked.
- /// The boundary value that must be less than or equal to .
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when the specified is less than , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustNotBeLessThan([NotNull, ValidatedNotNull] this T parameter, T other, Func exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract -- caller might have NRTs off
- if (parameter is null || parameter.CompareTo(other) < 0)
- Throw.CustomException(exceptionFactory, parameter!, other);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is not less than the given value, or otherwise throws an .
- ///
- /// The comparable to be checked.
- /// The boundary value that must be less than or equal to .
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when the specified is less than .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustBeGreaterThanOrEqualTo([NotNull, ValidatedNotNull] this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (parameter.MustNotBeNullReference(parameterName, message).CompareTo(other) < 0)
- Throw.MustBeGreaterThanOrEqualTo(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is not less than the given value, or otherwise throws your custom exception.
- ///
- /// The comparable to be checked.
- /// The boundary value that must be less than or equal to .
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when the specified is less than , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustBeGreaterThanOrEqualTo([NotNull, ValidatedNotNull] this T parameter, T other, Func exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || parameter.CompareTo(other) < 0)
- Throw.CustomException(exceptionFactory, parameter!, other);
- return parameter;
- }
-
- /*
- * -------------------------------------
- * Must Be Less Than
- * Must Not Be Greater Than or Equal To
- * -------------------------------------
- */
- ///
- /// Ensures that the specified is less than the given value, or otherwise throws an .
- ///
- /// The comparable to be checked.
- /// The boundary value that must be greater than .
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when the specified is not less than .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustBeLessThan([NotNull, ValidatedNotNull] this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (parameter.MustNotBeNullReference(parameterName, message).CompareTo(other) >= 0)
- Throw.MustBeLessThan(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is less than the given value, or otherwise throws your custom exception.
- ///
- /// The comparable to be checked.
- /// The boundary value that must be greater than .
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when the specified is not less than , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustBeLessThan([NotNull, ValidatedNotNull] this T parameter, T other, Func exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || parameter.CompareTo(other) >= 0)
- Throw.CustomException(exceptionFactory, parameter!, other);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is less than the given value, or otherwise throws an .
- ///
- /// The comparable to be checked.
- /// The boundary value that must be greater than .
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when the specified is not less than .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustNotBeGreaterThanOrEqualTo([NotNull, ValidatedNotNull] this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (parameter.MustNotBeNullReference(parameterName, message).CompareTo(other) >= 0)
- Throw.MustNotBeGreaterThanOrEqualTo(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is less than the given value, or otherwise throws your custom exception.
- ///
- /// The comparable to be checked.
- /// The boundary value that must be greater than .
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when the specified is not less than , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustNotBeGreaterThanOrEqualTo([NotNull, ValidatedNotNull] this T parameter, T other, Func exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || parameter.CompareTo(other) >= 0)
- Throw.CustomException(exceptionFactory, parameter!, other);
- return parameter;
- }
-
- /*
- * -------------------------------------
- * Must Be Greater Than
- * Must Not Be Less Than or Equal To
- * -------------------------------------
- */
- ///
- /// Ensures that the specified is greater than the given value, or otherwise throws an .
- ///
- /// The comparable to be checked.
- /// The boundary value that must be less than .
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when the specified is less than or equal to .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustBeGreaterThan([NotNull, ValidatedNotNull] this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (parameter.MustNotBeNullReference(parameterName, message).CompareTo(other) <= 0)
- Throw.MustBeGreaterThan(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is greater than the given value, or otherwise throws your custom exception.
- ///
- /// The comparable to be checked.
- /// The boundary value that must be less than .
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when the specified is less than or equal to , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustBeGreaterThan([NotNull, ValidatedNotNull] this T parameter, T other, Func exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || parameter.CompareTo(other) <= 0)
- Throw.CustomException(exceptionFactory, parameter!, other);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is greater than the given value, or otherwise throws an .
- ///
- /// The comparable to be checked.
- /// The boundary value that must be less than .
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when the specified is less than or equal to .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustNotBeLessThanOrEqualTo([NotNull, ValidatedNotNull] this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (parameter.MustNotBeNullReference(parameterName, message).CompareTo(other) <= 0)
- Throw.MustNotBeLessThanOrEqualTo(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is greater than the given value, or otherwise throws your custom exception.
- ///
- /// The comparable to be checked.
- /// The boundary value that must be less than .
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when the specified is less than or equal to , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustNotBeLessThanOrEqualTo([NotNull, ValidatedNotNull] this T parameter, T other, Func exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || parameter.CompareTo(other) <= 0)
- Throw.CustomException(exceptionFactory, parameter!, other);
- return parameter;
- }
-
- /*
- * -------------------------------------
- * Must Not Be Greater Than
- * Must Be Less Than or Equal To
- * -------------------------------------
- */
- ///
- /// Ensures that the specified is not greater than the given value, or otherwise throws an .
- ///
- /// The comparable to be checked.
- /// The boundary value that must be greater than or equal to .
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when the specified is greater than .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustNotBeGreaterThan([NotNull, ValidatedNotNull] this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (parameter.MustNotBeNullReference(parameterName, message).CompareTo(other) > 0)
- Throw.MustNotBeGreaterThan(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is not greater than the given value, or otherwise throws your custom exception.
- ///
- /// The comparable to be checked.
- /// The boundary value that must be greater than or equal to .
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when the specified is greater than , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustNotBeGreaterThan([NotNull, ValidatedNotNull] this T parameter, T other, Func exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || parameter.CompareTo(other) > 0)
- Throw.CustomException(exceptionFactory, parameter!, other);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is not greater than the given value, or otherwise throws an .
- ///
- /// The comparable to be checked.
- /// The boundary value that must be greater than or equal to .
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when the specified is greater than .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustBeLessThanOrEqualTo([NotNull, ValidatedNotNull] this T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (parameter.MustNotBeNullReference(parameterName, message).CompareTo(other) > 0)
- Throw.MustBeLessThanOrEqualTo(parameter, other, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified is not greater than the given value, or otherwise throws your custom exception.
- ///
- /// The comparable to be checked.
- /// The boundary value that must be greater than or equal to .
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when the specified is greater than , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustBeLessThanOrEqualTo([NotNull, ValidatedNotNull] this T parameter, T other, Func exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || parameter.CompareTo(other) > 0)
- Throw.CustomException(exceptionFactory, parameter!, other);
- return parameter;
- }
-
- /*
- * -------------------------------------
- * Ranges
- * -------------------------------------
- */
- ///
- /// Checks if the value is within the specified range.
- ///
- /// The comparable to be checked.
- /// The range where must be in-between.
- /// True if the parameter is within the specified range, else false.
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsIn([NotNull, ValidatedNotNull] this T parameter, Range range) where T : IComparable => range.IsValueWithinRange(parameter);
-
- ///
- /// Checks if the value is not within the specified range.
- ///
- /// The comparable to be checked.
- /// The range where must not be in-between.
- /// True if the parameter is not within the specified range, else false.
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsNotIn([NotNull, ValidatedNotNull] this T parameter, Range range) where T : IComparable => !range.IsValueWithinRange(parameter);
-
- ///
- /// Ensures that is within the specified range, or otherwise throws an .
- ///
- /// The type of the parameter to be checked.
- /// The parameter to be checked.
- /// The range where must be in-between.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is not within .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustBeIn([NotNull, ValidatedNotNull] this T parameter, Range range, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (!range.IsValueWithinRange(parameter.MustNotBeNullReference(parameterName, message)))
- Throw.MustBeInRange(parameter, range, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that is within the specified range, or otherwise throws your custom exception.
- ///
- /// The parameter to be checked.
- /// The range where must be in-between.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when is not within , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustBeIn([NotNull, ValidatedNotNull] this T parameter, Range range, Func, Exception> exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || !range.IsValueWithinRange(parameter))
- Throw.CustomException(exceptionFactory, parameter!, range);
- return parameter;
- }
-
- ///
- /// Ensures that is not within the specified range, or otherwise throws an .
- ///
- /// The type of the parameter to be checked.
- /// The parameter to be checked.
- /// The range where must not be in-between.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is within .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static T MustNotBeIn([NotNull, ValidatedNotNull] this T parameter, Range range, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable
- {
- if (range.IsValueWithinRange(parameter.MustNotBeNullReference(parameterName, message)))
- Throw.MustNotBeInRange(parameter, range, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that is not within the specified range, or otherwise throws your custom exception.
- ///
- /// The parameter to be checked.
- /// The range where must not be in-between.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when is within , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
- public static T MustNotBeIn([NotNull, ValidatedNotNull] this T parameter, Range range, Func, Exception> exceptionFactory) where T : IComparable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (parameter is null || range.IsValueWithinRange(parameter))
- Throw.CustomException(exceptionFactory, parameter!, range);
- return parameter;
- }
-}
\ No newline at end of file
diff --git a/Code/Light.GuardClauses/Check.Contains.cs b/Code/Light.GuardClauses/Check.Contains.cs
new file mode 100644
index 0000000..5e5c9f2
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.Contains.cs
@@ -0,0 +1,28 @@
+using System;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the string contains the specified value using the given comparison type.
+ ///
+ /// The string to be checked.
+ /// The other string.
+ /// One of the enumeration values that specifies the rules for the search.
+ /// True if contains , else false.
+ /// Thrown when or is null.
+ /// Thrown when is not a valid value.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("string:null => halt; value:null => halt")]
+ public static bool Contains(
+ // ReSharper disable once RedundantNullableFlowAttribute -- Caller might have NRTs turned off
+ [NotNull] [ValidatedNotNull] this string @string,
+ string value,
+ StringComparison comparisonType
+ ) =>
+ @string.MustNotBeNull(nameof(@string)).IndexOf(value.MustNotBeNull(nameof(value)), comparisonType) >= 0;
+}
diff --git a/Code/Light.GuardClauses/Check.DateTimeAssertions.cs b/Code/Light.GuardClauses/Check.DateTimeAssertions.cs
deleted file mode 100644
index feee6b2..0000000
--- a/Code/Light.GuardClauses/Check.DateTimeAssertions.cs
+++ /dev/null
@@ -1,99 +0,0 @@
-using System;
-using JetBrains.Annotations;
-using System.Runtime.CompilerServices;
-using Light.GuardClauses.Exceptions;
-
-namespace Light.GuardClauses;
-
-public static partial class Check
-{
- ///
- /// Ensures that the specified uses , or otherwise throws an .
- ///
- /// The date time to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not use .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static DateTime MustBeUtc(this DateTime parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Kind != DateTimeKind.Utc)
- Throw.MustBeUtcDateTime(parameter, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified uses , or otherwise throws your custom exception.
- ///
- /// The date time to be checked.
- /// The delegate that creates your custom exception. is passed to this delegate.
- /// Your custom exception thrown when does not use .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("exceptionFactory:null => halt")]
- public static DateTime MustBeUtc(this DateTime parameter, Func exceptionFactory)
- {
- if (parameter.Kind != DateTimeKind.Utc)
- Throw.CustomException(exceptionFactory, parameter);
- return parameter;
- }
-
- ///
- /// Ensures that the specified uses , or otherwise throws an .
- ///
- /// The date time to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not use .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static DateTime MustBeLocal(this DateTime parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Kind != DateTimeKind.Local)
- Throw.MustBeLocalDateTime(parameter, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified uses , or otherwise throws your custom exception.
- ///
- /// The date time to be checked.
- /// The delegate that creates your custom exception. is passed to this delegate.
- /// Your custom exception thrown when does not use .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("exceptionFactory:null => halt")]
- public static DateTime MustBeLocal(this DateTime parameter, Func exceptionFactory)
- {
- if (parameter.Kind != DateTimeKind.Local)
- Throw.CustomException(exceptionFactory, parameter);
- return parameter;
- }
-
- ///
- /// Ensures that the specified uses , or otherwise throws an .
- ///
- /// The date time to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not use .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static DateTime MustBeUnspecified(this DateTime parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Kind != DateTimeKind.Unspecified)
- Throw.MustBeUnspecifiedDateTime(parameter, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the specified uses , or otherwise throws your custom exception.
- ///
- /// The date time to be checked.
- /// The delegate that creates your custom exception. is passed to this delegate.
- /// Your custom exception thrown when does not use .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("exceptionFactory:null => halt")]
- public static DateTime MustBeUnspecified(this DateTime parameter, Func exceptionFactory)
- {
- if (parameter.Kind != DateTimeKind.Unspecified)
- Throw.CustomException(exceptionFactory, parameter);
- return parameter;
- }
-}
\ No newline at end of file
diff --git a/Code/Light.GuardClauses/Check.DerivesFrom.cs b/Code/Light.GuardClauses/Check.DerivesFrom.cs
new file mode 100644
index 0000000..8be3fd3
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.DerivesFrom.cs
@@ -0,0 +1,76 @@
+#if NET8_0
+using System.Diagnostics.CodeAnalysis;
+#endif
+using System;
+using System.Collections.Generic;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+// ReSharper disable RedundantNullableFlowAttribute -- NotNull has an effect, see Issue72NotNullAttributeTests
+
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified type derives from the other type. Internally, this method uses
+ /// by default so that constructed generic types and their corresponding generic type definitions are regarded as equal.
+ ///
+ /// The type info to be checked.
+ /// The base class that should derive from.
+ /// Thrown when or is null.
+ [ContractAnnotation("type:null => halt; baseClass:null => halt")]
+ public static bool DerivesFrom(
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type baseClass
+ )
+ {
+ baseClass.MustNotBeNull(nameof(baseClass));
+
+ var currentBaseType = type.MustNotBeNull(nameof(type)).BaseType;
+ while (currentBaseType != null)
+ {
+ if (currentBaseType.IsEquivalentTypeTo(baseClass))
+ {
+ return true;
+ }
+
+ currentBaseType = currentBaseType.BaseType;
+ }
+
+ return false;
+ }
+
+ ///
+ /// Checks if the specified type derives from the other type. This overload uses the specified
+ /// to compare the types.
+ ///
+ /// The type info to be checked.
+ /// The base class that should derive from.
+ /// The equality comparer used to compare the types.
+ /// Thrown when , or , or is null.
+ [ContractAnnotation("type:null => halt; baseClass:null => halt; typeComparer:null => halt")]
+ public static bool DerivesFrom(
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type baseClass,
+ [NotNull] [ValidatedNotNull] IEqualityComparer typeComparer
+ )
+ {
+ baseClass.MustNotBeNull(nameof(baseClass));
+ typeComparer.MustNotBeNull(nameof(typeComparer));
+
+ var currentBaseType = type.MustNotBeNull(nameof(type)).BaseType;
+ while (currentBaseType != null)
+ {
+ if (typeComparer.Equals(currentBaseType, baseClass))
+ {
+ return true;
+ }
+
+ currentBaseType = currentBaseType.BaseType;
+ }
+
+ return false;
+ }
+}
diff --git a/Code/Light.GuardClauses/Check.EnumerableAssertions.cs b/Code/Light.GuardClauses/Check.EnumerableAssertions.cs
deleted file mode 100644
index a67209b..0000000
--- a/Code/Light.GuardClauses/Check.EnumerableAssertions.cs
+++ /dev/null
@@ -1,661 +0,0 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
-using System.Linq;
-using System.Runtime.CompilerServices;
-using JetBrains.Annotations;
-using Light.GuardClauses.Exceptions;
-using Light.GuardClauses.FrameworkExtensions;
-using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
-
-namespace Light.GuardClauses;
-
-public static partial class Check
-{
- ///
- /// Ensures that the collection has the specified number of items, or otherwise throws an .
- ///
- /// The collection to be checked.
- /// The number of items the collection must have.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not have the specified number of items.
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustHaveCount([NotNull, ValidatedNotNull] this TCollection? parameter, int count, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where TCollection : class, IEnumerable
- {
- if (parameter!.Count(parameterName, message) != count)
- Throw.InvalidCollectionCount(parameter, count, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the collection has the specified number of items, or otherwise throws your custom exception.
- ///
- /// The collection to be checked.
- /// The number of items the collection must have.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when does not have the specified number of items, or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustHaveCount([NotNull, ValidatedNotNull] this TCollection? parameter, int count, Func exceptionFactory) where TCollection : class, IEnumerable
- {
- if (parameter is null || parameter.Count() != count)
- Throw.CustomException(exceptionFactory, parameter, count);
- return parameter;
- }
-
- ///
- /// Checks if the specified collection is null or empty.
- ///
- /// The collection to be checked.
- /// True if the collection is null or empty, else false.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("=> true, collection:canbenull; => false, collection:notnull")]
- public static bool IsNullOrEmpty([NotNullWhen(false)] this IEnumerable? collection) =>
- collection is null || collection.Count() == 0;
-
- ///
- /// Ensures that the collection is not null or empty, or otherwise throws an .
- ///
- /// The collection to be checked.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when has no items.
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustNotBeNullOrEmpty([NotNull, ValidatedNotNull] this TCollection? parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where TCollection : class, IEnumerable
- {
- if (parameter.Count(parameterName, message) == 0)
- Throw.EmptyCollection(parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the collection is not null or empty, or otherwise throws your custom exception.
- ///
- /// The collection to be checked.
- /// The delegate that creates your custom exception.
- /// Thrown when has no items, or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustNotBeNullOrEmpty([NotNull, ValidatedNotNull] this TCollection? parameter, Func exceptionFactory) where TCollection : class, IEnumerable
- {
- if (parameter is null || parameter.Count() == 0)
- Throw.CustomException(exceptionFactory, parameter);
- return parameter;
- }
-
- ///
- /// Ensures that the collection contains the specified item, or otherwise throws a .
- ///
- /// The collection to be checked.
- /// The item that must be part of the collection.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not contain .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustContain([NotNull, ValidatedNotNull] this TCollection? parameter, TItem item, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where TCollection : class, IEnumerable
- {
- if (parameter is ICollection collection)
- {
- if (!collection.Contains(item))
- Throw.MissingItem(parameter, item, parameterName, message);
- return parameter;
- }
-
- if (!parameter.MustNotBeNull(parameterName, message).Contains(item))
- Throw.MissingItem(parameter, item, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the collection contains the specified item, or otherwise throws your custom exception.
- ///
- /// The collection to be checked.
- /// The item that must be part of the collection.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when does not contain , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustContain([NotNull, ValidatedNotNull] this TCollection? parameter, TItem item, Func exceptionFactory) where TCollection : class, IEnumerable
- {
- if (parameter is ICollection collection)
- {
- if (!collection.Contains(item))
- Throw.CustomException(exceptionFactory, parameter, item);
- return parameter;
- }
-
- if (parameter is null || !parameter.Contains(item))
- Throw.CustomException(exceptionFactory, parameter, item);
- return parameter;
- }
-
- ///
- /// Ensures that the collection does not contain the specified item, or otherwise throws an .
- ///
- /// The collection to be checked.
- /// The item that must not be part of the collection.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when contains .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustNotContain([NotNull, ValidatedNotNull] this TCollection? parameter, TItem item, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where TCollection : class, IEnumerable
- {
- if (parameter is ICollection collection)
- {
- if (collection.Contains(item))
- Throw.ExistingItem(parameter, item, parameterName, message);
- return parameter;
- }
-
- if (parameter.MustNotBeNull(parameterName, message).Contains(item))
- Throw.ExistingItem(parameter, item, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the collection does not contain the specified item, or otherwise throws your custom exception.
- ///
- /// The collection to be checked.
- /// The item that must not be part of the collection.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when contains .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustNotContain([NotNull, ValidatedNotNull] this TCollection? parameter, TItem item, Func exceptionFactory) where TCollection : class, IEnumerable
- {
- if (parameter is ICollection collection)
- {
- if (collection.Contains(item))
- Throw.CustomException(exceptionFactory, parameter, item);
- return parameter;
- }
-
- if (parameter is null || parameter.Contains(item))
- Throw.CustomException(exceptionFactory, parameter, item);
- return parameter;
- }
-
- ///
- /// Checks if the given is one of the specified .
- ///
- /// The item to be checked.
- /// The collection that might contain the .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("items:null => halt")]
- // ReSharper disable once RedundantNullableFlowAttribute - the attribute has an effect, see Issue72NotNullAttribute tests
- public static bool IsOneOf(this TItem item, [NotNull, ValidatedNotNull] IEnumerable items)
- {
- if (items is ICollection collection)
- return collection.Contains(item);
-
- if (items is string @string && item is char character)
- return @string.IndexOf(character) != -1;
-
- return items.MustNotBeNull(nameof(items)).ContainsViaForeach(item);
- }
-
- ///
- /// Ensures that the value is one of the specified items, or otherwise throws a .
- ///
- /// The value to be checked.
- /// The items that should contain the value.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is not equal to one of the specified .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("items:null => halt")]
- // ReSharper disable once RedundantNullableFlowAttribute - the attribute has an effect, see Issue72NotNullAttribute tests
- public static TItem MustBeOneOf(this TItem parameter, [NotNull, ValidatedNotNull] IEnumerable items, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- // ReSharper disable PossibleMultipleEnumeration
- if (!parameter.IsOneOf(items.MustNotBeNull(nameof(items), message)))
- Throw.ValueNotOneOf(parameter, items, parameterName, message);
- return parameter;
- // ReSharper restore PossibleMultipleEnumeration
- }
-
- ///
- /// Ensures that the value is one of the specified items, or otherwise throws your custom exception.
- ///
- /// The value to be checked.
- /// The items that should contain the value.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when is not equal to one of the specified , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("items:null => halt")]
- public static TItem MustBeOneOf(this TItem parameter, [NotNull, ValidatedNotNull] TCollection items, Func exceptionFactory) where TCollection : class, IEnumerable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (items is null || !parameter.IsOneOf(items))
- Throw.CustomException(exceptionFactory, parameter, items!);
- return parameter;
- }
-
- ///
- /// Ensures that the value is not one of the specified items, or otherwise throws a .
- ///
- /// The value to be checked.
- /// The items that must not contain the value.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is equal to one of the specified .
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("items:null => halt")]
- // ReSharper disable once RedundantNullableFlowAttribute - the attribute has an effect, see Issue72NotNullAttribute tests
- public static TItem MustNotBeOneOf(this TItem parameter, [NotNull, ValidatedNotNull] IEnumerable items, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- // ReSharper disable PossibleMultipleEnumeration
- if (parameter.IsOneOf(items.MustNotBeNull(nameof(items), message)))
- Throw.ValueIsOneOf(parameter, items, parameterName, message);
- return parameter;
- // ReSharper restore PossibleMultipleEnumeration
- }
-
- ///
- /// Ensures that the value is not one of the specified items, or otherwise throws your custom exception.
- ///
- /// The value to be checked.
- /// The items that must not contain the value.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when is equal to one of the specified , or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("items:null => halt")]
- public static TItem MustNotBeOneOf(this TItem parameter, [NotNull, ValidatedNotNull] TCollection items, Func exceptionFactory) where TCollection : class, IEnumerable
- {
- // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
- if (items is null || parameter.IsOneOf(items))
- Throw.CustomException(exceptionFactory, parameter, items!);
- return parameter;
- }
-
- ///
- /// Ensures that the collection has at least the specified number of items, or otherwise throws an .
- ///
- /// The collection to be checked.
- /// The number of items the collection should have at least.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not contain at least the specified number of items.
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustHaveMinimumCount([NotNull, ValidatedNotNull] this TCollection? parameter, int count, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where TCollection : class, IEnumerable
- {
- if (parameter.Count(parameterName, message) < count)
- Throw.InvalidMinimumCollectionCount(parameter, count, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the collection has at least the specified number of items, or otherwise throws your custom exception.
- ///
- /// The collection to be checked.
- /// The number of items the collection should have at least.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when does not contain at least the specified number of items, or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustHaveMinimumCount([NotNull, ValidatedNotNull] this TCollection? parameter, int count, Func exceptionFactory) where TCollection : class, IEnumerable
- {
- if (parameter is null || parameter.Count() < count)
- Throw.CustomException(exceptionFactory, parameter, count);
- return parameter;
- }
-
- ///
- /// Ensures that the collection has at most the specified number of items, or otherwise throws an .
- ///
- /// The collection to be checked.
- /// The number of items the collection should have at most.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not contain at most the specified number of items.
- /// Thrown when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustHaveMaximumCount([NotNull, ValidatedNotNull] this TCollection? parameter, int count, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where TCollection : class, IEnumerable
- {
- if (parameter.Count(parameterName, message) > count)
- Throw.InvalidMaximumCollectionCount(parameter, count, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the collection has at most the specified number of items, or otherwise throws your custom exception.
- ///
- /// The collection to be checked.
- /// The number of items the collection should have at most.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when does not contain at most the specified number of items, or when is null.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static TCollection MustHaveMaximumCount([NotNull, ValidatedNotNull] this TCollection? parameter, int count, Func exceptionFactory) where TCollection : class, IEnumerable
- {
- if (parameter is null || parameter.Count() > count)
- Throw.CustomException(exceptionFactory, parameter, count);
- return parameter;
- }
-
- ///
- /// Ensures that the span has the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The length that the span must have.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not have the specified length.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustHaveLength(this Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length != length)
- Throw.InvalidSpanLength(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span has the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length that the span must have.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when does not have the specified length.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustHaveLength(this Span parameter, int length, SpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length != length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span has the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length that the span must have.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Your custom exception thrown when does not have the specified length.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustHaveLength(this ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length != length)
- Throw.InvalidSpanLength(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span has the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length that the span must have.
- /// The delegate that creates your custom exception. and are passed to this delegate.
- /// Your custom exception thrown when does not have the specified length.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustHaveLength(this ReadOnlySpan parameter, int length, ReadOnlySpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length != length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span is longer than the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The value that the span must be longer than.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is shorter than or equal to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustBeLongerThan(this Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length <= length)
- Throw.SpanMustBeLongerThan(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span is longer than the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length value that the span must be longer than.
- /// The delegate that creates your custom exception. and are passed to it.
- /// Your custom exception thrown when is shorter than or equal to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustBeLongerThan(this Span parameter, int length, SpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length <= length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span is longer than the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The value that the span must be longer than.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is shorter than or equal to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustBeLongerThan(this ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length <= length)
- Throw.SpanMustBeLongerThan(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span is longer than the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length value that the span must be longer than.
- /// The delegate that creates your custom exception. and are passed to it.
- /// Your custom exception thrown when is shorter than or equal to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustBeLongerThan(this ReadOnlySpan parameter, int length, ReadOnlySpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length <= length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span is longer than or equal to the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The value that the span must be longer than or equal to.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is shorter than .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustBeLongerThanOrEqualTo(this Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length < length)
- Throw.SpanMustBeLongerThanOrEqualTo(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span is longer than or equal to the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The value that the span must be longer than or equal to.
- /// The delegate that creates your custom exception. and are passed to it.
- /// Your custom exception thrown when is shorter than .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustBeLongerThanOrEqualTo(this Span parameter, int length, SpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length < length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span is longer than or equal to the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The value that the span must be longer than or equal to.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is shorter than .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustBeLongerThanOrEqualTo(this ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length < length)
- Throw.SpanMustBeLongerThanOrEqualTo(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span is longer than or equal to the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The value that the span must be longer than or equal to.
- /// The delegate that creates your custom exception. and are passed to it.
- /// Your custom exception thrown when is shorter than .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustBeLongerThanOrEqualTo(this ReadOnlySpan parameter, int length, ReadOnlySpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length < length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span is shorter than the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The length value that the span must be shorter than.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is longer than or equal to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustBeShorterThan(this Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length >= length)
- Throw.SpanMustBeShorterThan(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span is shorter than the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length value that the span must be shorter than.
- /// The delegate that creates your custom exception. and are passed to it.
- /// Your custom exception thrown when is longer than or equal to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustBeShorterThan(this Span parameter, int length, SpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length >= length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span is shorter than the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The length value that the span must be shorter than.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is longer than or equal to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustBeShorterThan(this ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length >= length)
- Throw.SpanMustBeShorterThan(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span is shorter than the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length value that the span must be shorter than.
- /// The delegate that creates your custom exception. and are passed to it.
- /// Your custom exception thrown when is longer than or equal to .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustBeShorterThan(this ReadOnlySpan parameter, int length, ReadOnlySpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length >= length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span is shorter than or equal to the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The length value that the span must be shorter than or equal to.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is longer than .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustBeShorterThanOrEqualTo(this Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length > length)
- Throw.SpanMustBeShorterThanOrEqualTo(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span is shorter than or equal to the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length value that the span must be shorter than or equal to.
- /// The delegate that creates your custom exception. and are passed to it.
- /// Your custom exception thrown when is longer than .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span MustBeShorterThanOrEqualTo(this Span parameter, int length, SpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length > length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-
- ///
- /// Ensures that the span is shorter than or equal to the specified length, or otherwise throws an .
- ///
- /// The span to be checked.
- /// The length value that the span must be shorter than or equal to.
- /// The name of the parameter (optional).
- /// The message that will be passed to the resulting exception (optional).
- /// Thrown when is longer than .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustBeShorterThanOrEqualTo(this ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
- {
- if (parameter.Length > length)
- Throw.SpanMustBeShorterThanOrEqualTo(parameter, length, parameterName, message);
- return parameter;
- }
-
- ///
- /// Ensures that the span is shorter than or equal to the specified length, or otherwise throws your custom exception.
- ///
- /// The span to be checked.
- /// The length value that the span must be shorter than or equal to.
- /// The delegate that creates your custom exception. and are passed to it.
- /// Your custom exception thrown when is longer than .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ReadOnlySpan MustBeShorterThanOrEqualTo(this ReadOnlySpan parameter, int length, ReadOnlySpanExceptionFactory exceptionFactory)
- {
- if (parameter.Length > length)
- Throw.CustomSpanException(exceptionFactory, parameter, length);
- return parameter;
- }
-}
\ No newline at end of file
diff --git a/Code/Light.GuardClauses/Check.Equals.cs b/Code/Light.GuardClauses/Check.Equals.cs
new file mode 100644
index 0000000..688e191
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.Equals.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Runtime.CompilerServices;
+using Light.GuardClauses.Exceptions;
+using Light.GuardClauses.FrameworkExtensions;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified strings are equal, using the given comparison rules.
+ ///
+ /// The first string to compare.
+ /// The second string to compare.
+ /// One of the enumeration values that specifies the rules for the comparison.
+ /// True if the two strings are considered equal, else false.
+ /// Thrown when is no valid enum value.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool Equals(this string? @string, string? value, StringComparisonType comparisonType)
+ {
+ if ((int) comparisonType < 6)
+ {
+ return string.Equals(@string, value, (StringComparison) comparisonType);
+ }
+
+ switch (comparisonType)
+ {
+ case StringComparisonType.OrdinalIgnoreWhiteSpace:
+ return @string.EqualsOrdinalIgnoreWhiteSpace(value);
+ case StringComparisonType.OrdinalIgnoreCaseIgnoreWhiteSpace:
+ return @string.EqualsOrdinalIgnoreCaseIgnoreWhiteSpace(value);
+ default:
+ Throw.EnumValueNotDefined(comparisonType, nameof(comparisonType));
+ return false;
+ }
+ }
+}
diff --git a/Code/Light.GuardClauses/Check.Implements.cs b/Code/Light.GuardClauses/Check.Implements.cs
new file mode 100644
index 0000000..e0643d4
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.Implements.cs
@@ -0,0 +1,79 @@
+#if NET8_0
+using System.Diagnostics.CodeAnalysis;
+#endif
+using System;
+using System.Collections.Generic;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+// ReSharper disable RedundantNullableFlowAttribute -- NotNull has an effect, see Issue72NotNullAttributeTests
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the type implements the specified interface type. Internally, this method uses
+ /// so that constructed generic types and their corresponding generic type definitions are regarded as equal.
+ ///
+ /// The type to be checked.
+ /// The interface type that should implement.
+ /// Thrown when or is null.
+ [ContractAnnotation("type:null => halt; interfaceType:null => halt")]
+ public static bool Implements(
+#if NET8_0
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
+#endif
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type interfaceType
+ )
+ {
+ type.MustNotBeNull();
+ interfaceType.MustNotBeNull();
+
+ var implementedInterfaces = type.GetInterfaces();
+ for (var i = 0; i < implementedInterfaces.Length; ++i)
+ {
+ if (interfaceType.IsEquivalentTypeTo(implementedInterfaces[i]))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ /// Checks if the type implements the specified interface type. This overload uses the specified
+ /// to compare the interface types.
+ ///
+ /// The type to be checked.
+ /// The interface type that should implement.
+ /// The equality comparer used to compare the interface types.
+ /// Thrown when , or , or is null.
+ [ContractAnnotation("type:null => halt; interfaceType:null => halt; typeComparer:null => halt")]
+ public static bool Implements(
+#if NET8_0
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
+#endif
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type interfaceType,
+ [NotNull] [ValidatedNotNull] IEqualityComparer typeComparer
+ )
+ {
+ type.MustNotBeNull();
+ interfaceType.MustNotBeNull();
+ typeComparer.MustNotBeNull();
+
+ var implementedInterfaces = type.GetInterfaces();
+ for (var i = 0; i < implementedInterfaces.Length; ++i)
+ {
+ if (typeComparer.Equals(implementedInterfaces[i], interfaceType))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/Code/Light.GuardClauses/Check.InheritsFrom.cs b/Code/Light.GuardClauses/Check.InheritsFrom.cs
new file mode 100644
index 0000000..6e2ad44
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.InheritsFrom.cs
@@ -0,0 +1,61 @@
+#if NET8_0
+using System.Diagnostics.CodeAnalysis;
+#endif
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+// ReSharper disable RedundantNullableFlowAttribute -- NotNull has an effect, see Issue72NotNullAttributeTests
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the given type derives from the specified base class or interface type. Internally, this method uses
+ /// so that constructed generic types and their corresponding generic type definitions are regarded as equal.
+ ///
+ /// The type to be checked.
+ /// The type describing an interface or base class that should derive from or implement.
+ /// Thrown when or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt; baseClassOrInterfaceType:null => halt")]
+ public static bool InheritsFrom(
+#if NET8_0
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
+#endif
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type baseClassOrInterfaceType
+ ) =>
+ baseClassOrInterfaceType
+ .MustNotBeNull(nameof(baseClassOrInterfaceType))
+ .IsInterface ?
+ type.Implements(baseClassOrInterfaceType) :
+ type.DerivesFrom(baseClassOrInterfaceType);
+
+ ///
+ /// Checks if the given type derives from the specified base class or interface type. This overload uses the specified
+ /// to compare the types.
+ ///
+ /// The type to be checked.
+ /// The type describing an interface or base class that should derive from or implement.
+ /// The equality comparer used to compare the types.
+ /// Thrown when , or , or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt; baseClassOrInterfaceType:null => halt; typeComparer:null => halt")]
+ public static bool InheritsFrom(
+#if NET8_0
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
+#endif
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type baseClassOrInterfaceType,
+ [NotNull] [ValidatedNotNull] IEqualityComparer typeComparer
+ ) =>
+ baseClassOrInterfaceType
+ .MustNotBeNull(nameof(baseClassOrInterfaceType))
+ .IsInterface ?
+ type.Implements(baseClassOrInterfaceType, typeComparer) :
+ type.DerivesFrom(baseClassOrInterfaceType, typeComparer);
+}
diff --git a/Code/Light.GuardClauses/Check.InvalidArgument.cs b/Code/Light.GuardClauses/Check.InvalidArgument.cs
new file mode 100644
index 0000000..cc50e69
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.InvalidArgument.cs
@@ -0,0 +1,58 @@
+using System;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using Light.GuardClauses.Exceptions;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified is true and throws an in this case.
+ ///
+ /// The condition to be checked. The exception is thrown when it is true.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the (optional).
+ /// Thrown when is true.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void InvalidArgument(bool condition, string? parameterName = null, string? message = null)
+ {
+ if (condition)
+ {
+ Throw.Argument(parameterName, message);
+ }
+ }
+
+ ///
+ /// Checks if the specified is true and throws your custom exception in this case.
+ ///
+ /// The condition to be checked. The exception is thrown when it is true.
+ /// The delegate that creates your custom exception.
+ /// Your custom exception thrown when is true.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("exceptionFactory:null => halt")]
+ public static void InvalidArgument(bool condition, Func exceptionFactory)
+ {
+ if (condition)
+ {
+ Throw.CustomException(exceptionFactory);
+ }
+ }
+
+ ///
+ /// Checks if the specified is true and throws your custom exception in this case.
+ ///
+ /// The condition to be checked. The exception is thrown when it is true.
+ /// The value that is checked in the . This value is passed to the .
+ /// The delegate that creates your custom exception. The is passed to this delegate.
+ /// Your custom exception thrown when is true.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("exceptionFactory:null => halt")]
+ public static void InvalidArgument(bool condition, T parameter, Func exceptionFactory)
+ {
+ if (condition)
+ {
+ Throw.CustomException(exceptionFactory, parameter);
+ }
+ }
+}
diff --git a/Code/Light.GuardClauses/Check.InvalidOperation.cs b/Code/Light.GuardClauses/Check.InvalidOperation.cs
new file mode 100644
index 0000000..c4ff5b7
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.InvalidOperation.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Runtime.CompilerServices;
+using Light.GuardClauses.Exceptions;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified is true and throws an in this case.
+ ///
+ /// The condition to be checked. The exception is thrown when it is true.
+ /// The message that will be passed to the (optional).
+ /// Thrown when is true.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void InvalidOperation(bool condition, string? message = null)
+ {
+ if (condition)
+ {
+ Throw.InvalidOperation(message);
+ }
+ }
+}
diff --git a/Code/Light.GuardClauses/Check.InvalidState.cs b/Code/Light.GuardClauses/Check.InvalidState.cs
new file mode 100644
index 0000000..1e89445
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.InvalidState.cs
@@ -0,0 +1,22 @@
+using System.Runtime.CompilerServices;
+using Light.GuardClauses.Exceptions;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified is true and throws an in this case.
+ ///
+ /// The condition to be checked. The exception is thrown when it is true.
+ /// The message that will be passed to the .
+ /// Thrown when is true.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void InvalidState(bool condition, string? message = null)
+ {
+ if (condition)
+ {
+ Throw.InvalidState(message);
+ }
+ }
+}
diff --git a/Code/Light.GuardClauses/Check.IsApproximately.cs b/Code/Light.GuardClauses/Check.IsApproximately.cs
new file mode 100644
index 0000000..698450e
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsApproximately.cs
@@ -0,0 +1,61 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified value is approximately the same as the other value, using the given tolerance.
+ ///
+ /// The first value to be compared.
+ /// The second value to be compared.
+ /// The tolerance indicating how much the two values may differ from each other.
+ ///
+ /// True if and are equal or if their absolute difference
+ /// is smaller than the given , otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsApproximately(this double value, double other, double tolerance) =>
+ Math.Abs(value - other) < tolerance;
+
+ ///
+ /// Checks if the specified value is approximately the same as the other value, using the default tolerance of 0.0001.
+ ///
+ /// The first value to be compared.
+ /// The second value to be compared.
+ ///
+ /// True if and are equal or if their absolute difference
+ /// is smaller than 0.0001, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsApproximately(this double value, double other) =>
+ Math.Abs(value - other) < 0.0001;
+
+ ///
+ /// Checks if the specified value is approximately the same as the other value, using the given tolerance.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ /// The tolerance indicating how much the two values may differ from each other.
+ ///
+ /// True if and are equal or if their absolute difference
+ /// is smaller than the given , otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsApproximately(this float value, float other, float tolerance) =>
+ Math.Abs(value - other) < tolerance;
+
+ ///
+ /// Checks if the specified value is approximately the same as the other value, using the default tolerance of 0.0001f.
+ ///
+ /// The first value to be compared.
+ /// The second value to be compared.
+ ///
+ /// True if and are equal or if their absolute difference
+ /// is smaller than 0.0001f, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsApproximately(this float value, float other) =>
+ Math.Abs(value - other) < 0.0001f;
+}
diff --git a/Code/Light.GuardClauses/Check.IsDigit.cs b/Code/Light.GuardClauses/Check.IsDigit.cs
new file mode 100644
index 0000000..fb7756a
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsDigit.cs
@@ -0,0 +1,12 @@
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified character is a digit.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsDigit(this char character) => char.IsDigit(character);
+}
diff --git a/Code/Light.GuardClauses/Check.IsEmailAddress.cs b/Code/Light.GuardClauses/Check.IsEmailAddress.cs
new file mode 100644
index 0000000..7cfb698
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsEmailAddress.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.Text.RegularExpressions;
+using JetBrains.Annotations;
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified string is an email address using the default email regular expression
+ /// defined in .
+ ///
+ /// The string to be checked if it is an email address.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("emailAddress:null => false")]
+ public static bool IsEmailAddress([NotNullWhen(true)] this string? emailAddress) =>
+ emailAddress != null && RegularExpressions.EmailRegex.IsMatch(emailAddress);
+
+ ///
+ /// Checks if the specified string is an email address using the provided regular expression for validation.
+ ///
+ /// The string to be checked.
+ /// The regular expression that determines whether the input string is an email address.
+ /// Thrown when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("emailAddress:null => false; emailAddressPattern:null => halt")]
+ public static bool IsEmailAddress([NotNullWhen(true)] this string? emailAddress, Regex emailAddressPattern) =>
+ emailAddress != null && emailAddressPattern.MustNotBeNull(nameof(emailAddressPattern)).IsMatch(emailAddress);
+}
diff --git a/Code/Light.GuardClauses/Check.IsEmpty.cs b/Code/Light.GuardClauses/Check.IsEmpty.cs
new file mode 100644
index 0000000..46574bb
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsEmpty.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified GUID is an empty one.
+ ///
+ /// The GUID to be checked.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsEmpty(this Guid parameter) => parameter == Guid.Empty;
+}
diff --git a/Code/Light.GuardClauses/Check.IsEquivalentTypeTo.cs b/Code/Light.GuardClauses/Check.IsEquivalentTypeTo.cs
new file mode 100644
index 0000000..585abaa
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsEquivalentTypeTo.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the two specified types are equivalent. This is true when both types are equal or
+ /// when one type is a constructed generic type and the other type is the corresponding generic type definition.
+ ///
+ /// The first type to be checked.
+ /// The other type to be checked.
+ ///
+ /// True if both types are null, or if both are equal, or if one type
+ /// is a constructed generic type and the other one is the corresponding generic type definition, else false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsEquivalentTypeTo(this Type? type, Type? other) =>
+ ReferenceEquals(type, other) ||
+ (type is not null &&
+ other is not null &&
+ (type == other ||
+ (type.IsConstructedGenericType != other.IsConstructedGenericType &&
+ CheckTypeEquivalency(type, other))));
+
+ private static bool CheckTypeEquivalency(Type type, Type other)
+ {
+ if (type.IsConstructedGenericType)
+ {
+ return type.GetGenericTypeDefinition() == other;
+ }
+
+ return other.GetGenericTypeDefinition() == type;
+ }
+}
diff --git a/Code/Light.GuardClauses/Check.IsGreaterThanOrApproximately.cs b/Code/Light.GuardClauses/Check.IsGreaterThanOrApproximately.cs
new file mode 100644
index 0000000..caca671
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsGreaterThanOrApproximately.cs
@@ -0,0 +1,60 @@
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified value is greater than or approximately the same as the other value, using the given tolerance.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ /// The tolerance indicating how much the two values may differ from each other.
+ ///
+ /// True if is greater than or if their absolute difference
+ /// is smaller than the given , otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsGreaterThanOrApproximately(this double value, double other, double tolerance) =>
+ value > other || value.IsApproximately(other, tolerance);
+
+ ///
+ /// Checks if the specified value is greater than or approximately the same as the other value, using the default tolerance of 0.0001.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ ///
+ /// True if is greater than or if their absolute difference
+ /// is smaller than 0.0001, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsGreaterThanOrApproximately(this double value, double other) =>
+ value > other || value.IsApproximately(other);
+
+ ///
+ /// Checks if the specified value is greater than or approximately the same as the other value, using the given tolerance.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ /// The tolerance indicating how much the two values may differ from each other.
+ ///
+ /// True if is greater than or if their absolute difference
+ /// is smaller than the given , otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsGreaterThanOrApproximately(this float value, float other, float tolerance) =>
+ value > other || value.IsApproximately(other, tolerance);
+
+ ///
+ /// Checks if the specified value is greater than or approximately the same as the other value, using the default tolerance of 0.0001f.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ ///
+ /// True if is greater than or if their absolute difference
+ /// is smaller than 0.0001, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsGreaterThanOrApproximately(this float value, float other) =>
+ value > other || value.IsApproximately(other);
+}
diff --git a/Code/Light.GuardClauses/Check.IsIn.cs b/Code/Light.GuardClauses/Check.IsIn.cs
new file mode 100644
index 0000000..b33484f
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsIn.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Runtime.CompilerServices;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the value is within the specified range.
+ ///
+ /// The comparable to be checked.
+ /// The range where must be in-between.
+ /// True if the parameter is within the specified range, else false.
+ /// Thrown when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsIn([NotNull] [ValidatedNotNull] this T parameter, Range range)
+ where T : IComparable =>
+ range.IsValueWithinRange(parameter);
+}
diff --git a/Code/Light.GuardClauses/Check.IsLessThanOrApproximately.cs b/Code/Light.GuardClauses/Check.IsLessThanOrApproximately.cs
new file mode 100644
index 0000000..c24c1a0
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsLessThanOrApproximately.cs
@@ -0,0 +1,60 @@
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified value is less than or approximately the same as the other value, using the given tolerance.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ /// The tolerance indicating how much the two values may differ from each other.
+ ///
+ /// True if is less than or if their absolute difference
+ /// is smaller than the given , otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLessThanOrApproximately(this double value, double other, double tolerance) =>
+ value < other || value.IsApproximately(other, tolerance);
+
+ ///
+ /// Checks if the specified value is less than or approximately the same as the other value, using the default tolerance of 0.0001.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ ///
+ /// True if is less than or if their absolute difference
+ /// is smaller than 0.0001, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLessThanOrApproximately(this double value, double other) =>
+ value < other || value.IsApproximately(other);
+
+ ///
+ /// Checks if the specified value is less than or approximately the same as the other value, using the given tolerance.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ /// The tolerance indicating how much the two values may differ from each other.
+ ///
+ /// True if is less than or if their absolute difference
+ /// is smaller than the given , otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLessThanOrApproximately(this float value, float other, float tolerance) =>
+ value < other || value.IsApproximately(other, tolerance);
+
+ ///
+ /// Checks if the specified value is less than or approximately the same as the other value, using the default tolerance of 0.0001f.
+ ///
+ /// The first value to compare.
+ /// The second value to compare.
+ ///
+ /// True if is less than or if their absolute difference
+ /// is smaller than 0.0001f, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLessThanOrApproximately(this float value, float other) =>
+ value < other || value.IsApproximately(other);
+}
diff --git a/Code/Light.GuardClauses/Check.IsLetter.cs b/Code/Light.GuardClauses/Check.IsLetter.cs
new file mode 100644
index 0000000..2639fc9
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsLetter.cs
@@ -0,0 +1,12 @@
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified character is a letter.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLetter(this char character) => char.IsLetter(character);
+}
diff --git a/Code/Light.GuardClauses/Check.IsLetterOrDigit.cs b/Code/Light.GuardClauses/Check.IsLetterOrDigit.cs
new file mode 100644
index 0000000..1acd030
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsLetterOrDigit.cs
@@ -0,0 +1,12 @@
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified character is a letter or digit.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLetterOrDigit(this char character) => char.IsLetterOrDigit(character);
+}
diff --git a/Code/Light.GuardClauses/Check.IsNewLine.cs b/Code/Light.GuardClauses/Check.IsNewLine.cs
new file mode 100644
index 0000000..80979ba
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsNewLine.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the string is either "\n" or "\r\n". This is done independently of the current value of .
+ ///
+ /// The string to be checked.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("=> false, parameter:canbenull; => true, parameter:notnull")]
+ public static bool IsNewLine([NotNullWhen(true)] this string? parameter) =>
+ parameter == "\n" || parameter == "\r\n";
+}
diff --git a/Code/Light.GuardClauses/Check.IsNotIn.cs b/Code/Light.GuardClauses/Check.IsNotIn.cs
new file mode 100644
index 0000000..3cf76a6
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsNotIn.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Runtime.CompilerServices;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the value is not within the specified range.
+ ///
+ /// The comparable to be checked.
+ /// The range where must not be in-between.
+ /// True if the parameter is not within the specified range, else false.
+ /// Thrown when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsNotIn([NotNull] [ValidatedNotNull] this T parameter, Range range)
+ where T : IComparable =>
+ !range.IsValueWithinRange(parameter);
+}
diff --git a/Code/Light.GuardClauses/Check.IsNullOrEmpty.cs b/Code/Light.GuardClauses/Check.IsNullOrEmpty.cs
new file mode 100644
index 0000000..d858001
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsNullOrEmpty.cs
@@ -0,0 +1,28 @@
+using System.Collections;
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using Light.GuardClauses.FrameworkExtensions;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified collection is null or empty.
+ ///
+ /// The collection to be checked.
+ /// True if the collection is null or empty, else false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("=> true, collection:canbenull; => false, collection:notnull")]
+ public static bool IsNullOrEmpty([NotNullWhen(false)] this IEnumerable? collection) =>
+ collection is null || collection.Count() == 0;
+
+ ///
+ /// Checks if the specified string is null or empty.
+ ///
+ /// The string to be checked.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("=> false, string:notnull; => true, string:canbenull")]
+ public static bool IsNullOrEmpty([NotNullWhen(false)] this string? @string) => string.IsNullOrEmpty(@string);
+}
diff --git a/Code/Light.GuardClauses/Check.IsNullOrWhiteSpace.cs b/Code/Light.GuardClauses/Check.IsNullOrWhiteSpace.cs
new file mode 100644
index 0000000..c67f171
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsNullOrWhiteSpace.cs
@@ -0,0 +1,17 @@
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified string is null, empty, or contains only white space.
+ ///
+ /// The string to be checked.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("=> false, string:notnull; => true, string:canbenull")]
+ public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? @string) =>
+ string.IsNullOrWhiteSpace(@string);
+}
diff --git a/Code/Light.GuardClauses/Check.IsOneOf.cs b/Code/Light.GuardClauses/Check.IsOneOf.cs
new file mode 100644
index 0000000..25b0063
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsOneOf.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using Light.GuardClauses.FrameworkExtensions;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the given is one of the specified .
+ ///
+ /// The item to be checked.
+ /// The collection that might contain the .
+ /// Thrown when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("items:null => halt")]
+ // ReSharper disable once RedundantNullableFlowAttribute - the attribute has an effect, see Issue72NotNullAttribute tests
+ public static bool IsOneOf(this TItem item, [NotNull] [ValidatedNotNull] IEnumerable items)
+ {
+ if (items is ICollection collection)
+ {
+ return collection.Contains(item);
+ }
+
+ if (items is string @string && item is char character)
+ {
+ return @string.IndexOf(character) != -1;
+ }
+
+ return items.MustNotBeNull(nameof(items)).ContainsViaForeach(item);
+ }
+}
diff --git a/Code/Light.GuardClauses/Check.IsOpenConstructedGenericType.cs b/Code/Light.GuardClauses/Check.IsOpenConstructedGenericType.cs
new file mode 100644
index 0000000..7d8348e
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsOpenConstructedGenericType.cs
@@ -0,0 +1,26 @@
+#if NET8_0
+using System.Diagnostics.CodeAnalysis;
+#endif
+using System;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the given is a generic type that has open generic parameters,
+ /// but is no generic type definition.
+ ///
+ /// The type to be checked.
+ /// Thrown when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt")]
+ // ReSharper disable once RedundantNullableFlowAttribute -- NotNull has an effect, see Issue72NotNullAttributeTests
+ public static bool IsOpenConstructedGenericType([NotNull] [ValidatedNotNull] this Type type) =>
+ type.MustNotBeNull(nameof(type)).IsGenericType &&
+ type.ContainsGenericParameters &&
+ type.IsGenericTypeDefinition == false;
+}
diff --git a/Code/Light.GuardClauses/Check.IsOrDerivesFrom.cs b/Code/Light.GuardClauses/Check.IsOrDerivesFrom.cs
new file mode 100644
index 0000000..ed74bee
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsOrDerivesFrom.cs
@@ -0,0 +1,48 @@
+#if NET8_0
+using System.Diagnostics.CodeAnalysis;
+#endif
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+// ReSharper disable RedundantNullableFlowAttribute -- NotNull has an effect, see Issue72NotNullAttributeTests
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the given is equal to the specified or if it derives from it. Internally, this
+ /// method uses so that constructed generic types and their corresponding generic type definitions are regarded as equal.
+ ///
+ /// The type to be checked.
+ /// The type that is equivalent to or the base class type where derives from.
+ /// Thrown when or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt; otherType:null => halt")]
+ public static bool IsOrDerivesFrom(
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type otherType
+ ) =>
+ type.IsEquivalentTypeTo(otherType.MustNotBeNull(nameof(otherType))) || type.DerivesFrom(otherType);
+
+ ///
+ /// Checks if the given is equal to the specified or if it derives from it. This overload uses the specified
+ /// to compare the types.
+ ///
+ /// The type to be checked.
+ /// The type that is equivalent to or the base class type where derives from.
+ /// The equality comparer used to compare the types.
+ /// Thrown when , or , or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt; otherType:null => halt; typeComparer:null => halt")]
+ public static bool IsOrDerivesFrom(
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type otherType,
+ [NotNull] [ValidatedNotNull] IEqualityComparer typeComparer
+ ) =>
+ typeComparer.MustNotBeNull(nameof(typeComparer)).Equals(type, otherType.MustNotBeNull(nameof(otherType))) ||
+ type.DerivesFrom(otherType, typeComparer);
+}
diff --git a/Code/Light.GuardClauses/Check.IsOrImplements.cs b/Code/Light.GuardClauses/Check.IsOrImplements.cs
new file mode 100644
index 0000000..aea0e10
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsOrImplements.cs
@@ -0,0 +1,58 @@
+#if NET8_0
+using System.Diagnostics.CodeAnalysis;
+#endif
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+// ReSharper disable RedundantNullableFlowAttribute -- NotNull has an effect, see Issue72NotNullAttributeTests
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the given is equal to the specified or if it implements it. Internally, this
+ /// method uses so that constructed generic types and their corresponding generic type definitions are regarded as equal.
+ ///
+ /// The type to be checked.
+ /// The type that is equivalent to or the interface type that implements.
+ /// Thrown when or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt; otherType:null => halt")]
+ public static bool IsOrImplements(
+#if NET8_0
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
+#endif
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type otherType
+ ) =>
+ type.IsEquivalentTypeTo(otherType.MustNotBeNull(nameof(otherType))) || type.Implements(otherType);
+
+ ///
+ /// Checks if the given is equal to the specified or if it implements it. This overload uses the specified
+ /// to compare the types.
+ ///
+ /// ,
+ /// The type to be checked.
+ /// The type that is equivalent to or the interface type that implements.
+ /// The equality comparer used to compare the interface types.
+ /// Thrown when or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt; otherType:null => halt")]
+ public static bool IsOrImplements(
+#if NET8_0
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
+#endif
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type otherType,
+ [NotNull] [ValidatedNotNull] IEqualityComparer typeComparer
+ ) =>
+ typeComparer.MustNotBeNull(nameof(typeComparer)).Equals(
+ type.MustNotBeNull(nameof(type)),
+ otherType.MustNotBeNull(nameof(otherType))
+ ) ||
+ type.Implements(otherType, typeComparer);
+}
diff --git a/Code/Light.GuardClauses/Check.IsOrInheritsFrom.cs b/Code/Light.GuardClauses/Check.IsOrInheritsFrom.cs
new file mode 100644
index 0000000..62549f4
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsOrInheritsFrom.cs
@@ -0,0 +1,56 @@
+#if NET8_0
+using System.Diagnostics.CodeAnalysis;
+#endif
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+// ReSharper disable RedundantNullableFlowAttribute -- NotNull has an effect, see Issue72NotNullAttributeTests
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the given is equal to the specified or if it derives from it or implements it.
+ /// Internally, this method uses so that constructed generic types and their corresponding generic type definitions
+ /// are regarded as equal.
+ ///
+ /// The type to be checked.
+ /// The type that is equivalent to or the base class type where derives from.
+ /// Thrown when or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt; otherType:null => halt")]
+ public static bool IsOrInheritsFrom(
+#if NET8_0
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
+#endif
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type otherType
+ ) =>
+ type.IsEquivalentTypeTo(otherType.MustNotBeNull(nameof(otherType))) || type.InheritsFrom(otherType);
+
+
+ ///
+ /// Checks if the given is equal to the specified or if it derives from it or implements it.
+ /// This overload uses the specified to compare the types.
+ ///
+ /// The type to be checked.
+ /// The type that is equivalent to or the base class type where derives from.
+ /// The equality comparer used to compare the types.
+ /// Thrown when or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("type:null => halt; otherType:null => halt; typeComparer:null => halt")]
+ public static bool IsOrInheritsFrom(
+#if NET8_0
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
+#endif
+ [NotNull] [ValidatedNotNull] this Type type,
+ [NotNull] [ValidatedNotNull] Type otherType,
+ [NotNull] [ValidatedNotNull] IEqualityComparer typeComparer
+ ) =>
+ typeComparer.MustNotBeNull(nameof(typeComparer)).Equals(type, otherType.MustNotBeNull(nameof(otherType))) ||
+ type.InheritsFrom(otherType, typeComparer);
+}
diff --git a/Code/Light.GuardClauses/Check.IsSameAs.cs b/Code/Light.GuardClauses/Check.IsSameAs.cs
new file mode 100644
index 0000000..8d4c063
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsSameAs.cs
@@ -0,0 +1,19 @@
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if and point to the same object.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ // ReSharper disable StringLiteralTypo
+ [ContractAnnotation(
+ "parameter:notNull => true, other:notnull; parameter:notNull => false, other:canbenull; other:notnull => true, parameter:notnull; other:notnull => false, parameter:canbenull"
+ )]
+ // ReSharper restore StringLiteralTypo
+ public static bool IsSameAs([NoEnumeration] this T? parameter, [NoEnumeration] T? other) where T : class =>
+ ReferenceEquals(parameter, other);
+}
diff --git a/Code/Light.GuardClauses/Check.IsSubstringOf.cs b/Code/Light.GuardClauses/Check.IsSubstringOf.cs
new file mode 100644
index 0000000..7812ef2
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsSubstringOf.cs
@@ -0,0 +1,46 @@
+using System;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the string is a substring of the other string.
+ ///
+ /// The string to be checked.
+ /// The other string.
+ /// True if is a substring of , else false.
+ /// Thrown when or is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("value:null => halt; other:null => halt")]
+ // ReSharper disable RedundantNullableFlowAttribute
+ public static bool IsSubstringOf(
+ [NotNull] [ValidatedNotNull] this string value,
+ [NotNull] [ValidatedNotNull] string other
+ ) =>
+ other.MustNotBeNull(nameof(other)).Contains(value);
+ // ReSharper restore RedundantNullableFlowAttribute
+
+ ///
+ /// Checks if the string is a substring of the other string.
+ ///
+ /// The string to be checked.
+ /// The other string.
+ /// One of the enumeration values that specifies the rules for the search.
+ /// True if is a substring of , else false.
+ /// Thrown when or is null.
+ /// Thrown when is not a valid value.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("value:null => halt; other:null => halt")]
+ // ReSharper disable RedundantNullableFlowAttribute
+ public static bool IsSubstringOf(
+ [NotNull] [ValidatedNotNull] this string value,
+ [NotNull] [ValidatedNotNull] string other,
+ StringComparison comparisonType
+ ) =>
+ other.MustNotBeNull(nameof(other)).IndexOf(value, comparisonType) != -1;
+ // ReSharper disable RedundantNullableFlowAttribute
+}
diff --git a/Code/Light.GuardClauses/Check.IsTrimmed.cs b/Code/Light.GuardClauses/Check.IsTrimmed.cs
new file mode 100644
index 0000000..727de57
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsTrimmed.cs
@@ -0,0 +1,39 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified string is trimmed, i.e. it does not start or end with
+ /// white space characters. Inputting an empty string will return true. When null is passed,
+ /// you can control the return value with which will
+ /// return true by default.
+ ///
+ /// The string to be checked.
+ ///
+ /// The value indicating whether true or false should be returned from this method when the
+ /// is null. The default value is true.
+ ///
+ ///
+ /// True if the is trimmed, else false. An empty string will result in true.
+ /// You can control the return value with when the
+ /// is null.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsTrimmed(this string? parameter, bool regardNullAsTrimmed = true) =>
+ parameter is null ? regardNullAsTrimmed : parameter.AsSpan().IsTrimmed();
+
+ ///
+ /// Checks if the specified character span is trimmed, i.e. it does not start or end with
+ /// white space characters. Inputting an empty span will return true.
+ ///
+ /// The character span to be checked.
+ /// True if the is trimmed, else false. An empty span will result in true.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsTrimmed(this ReadOnlySpan parameter) =>
+ parameter.Length == 0 ||
+ !parameter[0].IsWhiteSpace() &&
+ !parameter[parameter.Length - 1].IsWhiteSpace();
+}
diff --git a/Code/Light.GuardClauses/Check.IsTrimmedAtEnd.cs b/Code/Light.GuardClauses/Check.IsTrimmedAtEnd.cs
new file mode 100644
index 0000000..644ba56
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsTrimmedAtEnd.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified string is trimmed at the end, i.e. it does not end with
+ /// white space characters. Inputting an empty string will return true.
+ ///
+ /// The string to be checked.
+ ///
+ /// The value indicating whether true or false should be returned from this method when the
+ /// is null. The default value is true.
+ ///
+ ///
+ /// True if the is trimmed at the end, else false.
+ /// An empty string will result in true.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsTrimmedAtEnd(this string? parameter, bool regardNullAsTrimmed = true) =>
+ parameter is null ? regardNullAsTrimmed : parameter.AsSpan().IsTrimmedAtEnd();
+
+ ///
+ /// Checks if the specified character span is trimmed at the end, i.e. it does not end with
+ /// white space characters. Inputting an empty span will return true.
+ ///
+ /// The character span to be checked.
+ ///
+ /// True if the is trimmed at the end, else false.
+ /// An empty span will result in true.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsTrimmedAtEnd(this ReadOnlySpan parameter) =>
+ parameter.Length == 0 || !parameter[parameter.Length - 1].IsWhiteSpace();
+}
diff --git a/Code/Light.GuardClauses/Check.IsTrimmedAtStart.cs b/Code/Light.GuardClauses/Check.IsTrimmedAtStart.cs
new file mode 100644
index 0000000..3e2e3d1
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsTrimmedAtStart.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified string is trimmed at the start, i.e. it does not start with
+ /// white space characters. Inputting an empty string will return true.
+ ///
+ /// The string to be checked.
+ ///
+ /// The value indicating whether true or false should be returned from this method when the
+ /// is null. The default value is true.
+ ///
+ ///
+ /// True if the is trimmed at the start, else false.
+ /// An empty string will result in true.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsTrimmedAtStart(this string? parameter, bool regardNullAsTrimmed = true) =>
+ parameter is null ? regardNullAsTrimmed : parameter.AsSpan().IsTrimmedAtStart();
+
+ ///
+ /// Checks if the specified character span is trimmed at the start, i.e. it does not start with
+ /// white space characters. Inputting an empty span will return true.
+ ///
+ /// The character span to be checked.
+ ///
+ /// True if the is trimmed at the start, else false.
+ /// An empty span will result in true.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsTrimmedAtStart(this ReadOnlySpan parameter) =>
+ parameter.Length == 0 || !parameter[0].IsWhiteSpace();
+}
diff --git a/Code/Light.GuardClauses/Check.IsValidEnumValue.cs b/Code/Light.GuardClauses/Check.IsValidEnumValue.cs
new file mode 100644
index 0000000..4869f25
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsValidEnumValue.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified value is a valid enum value of its type. This is true when the specified value
+ /// is one of the constants defined in the enum, or a valid flags combination when the enum type is marked
+ /// with the .
+ ///
+ /// The type of the enum.
+ /// The enum value to be checked.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsValidEnumValue(this T parameter) where T : struct, Enum =>
+ EnumInfo.IsValidEnumValue(parameter);
+}
diff --git a/Code/Light.GuardClauses/Check.IsWhiteSpace.cs b/Code/Light.GuardClauses/Check.IsWhiteSpace.cs
new file mode 100644
index 0000000..4a18250
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.IsWhiteSpace.cs
@@ -0,0 +1,12 @@
+using System.Runtime.CompilerServices;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Checks if the specified character is a white space character.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsWhiteSpace(this char character) => char.IsWhiteSpace(character);
+}
diff --git a/Code/Light.GuardClauses/Check.MustBe.cs b/Code/Light.GuardClauses/Check.MustBe.cs
new file mode 100644
index 0000000..722e499
--- /dev/null
+++ b/Code/Light.GuardClauses/Check.MustBe.cs
@@ -0,0 +1,209 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
+using Light.GuardClauses.Exceptions;
+
+namespace Light.GuardClauses;
+
+public static partial class Check
+{
+ ///
+ /// Ensures that is equal to using the default equality comparer, or otherwise throws a .
+ ///
+ /// The first value to be compared.
+ /// The other value to be compared.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// Thrown when and are not equal.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static T MustBe(
+ this T parameter,
+ T other,
+ [CallerArgumentExpression("parameter")] string? parameterName = null,
+ string? message = null
+ )
+ {
+ if (!EqualityComparer.Default.Equals(parameter, other))
+ {
+ Throw.ValuesNotEqual(parameter, other, parameterName, message);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that is equal to using the default equality comparer, or otherwise throws your custom exception.
+ ///
+ /// The first value to be compared.
+ /// The other value to be compared.
+ /// The delegate that creates your custom exception. and are passed to this delegate.
+ /// Your custom exception thrown when and are not equal.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static T MustBe(this T parameter, T other, Func exceptionFactory)
+ {
+ if (!EqualityComparer.Default.Equals(parameter, other))
+ {
+ Throw.CustomException(exceptionFactory, parameter, other);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that