C# extension methods for Base Class Library types.
The motivation behind this project is to collect reusable extension methods into a NuGet package avoiding CPD (copy-paste-development).
You can find the complete documentation here.
The documentation is generated by Sandcastle Help File Builder.
You can download the package from nuget.org.
You can find the release notes here.
There are examples below only for some of the methods. Feel free to discover all the methods in the documentation.
- For generic
T
type:ThrowIfNull<T>()
throwsArgumentNullException
- For
System.String
type:ThrowIfNullOrEmpty()
throwsArgumentNullException
orArgumentException
ThrowIfNullOrWhiteSpace()
throwsArgumentNullException
orArgumentException
- For
System.Guid
type:ThrowIfEmptyGuid()
throwsArgumentException
Would like to use the new static methods in ArgumentOutOfRangeException
that were introduced in .NET 8 but still on older target framework?
In this package they are available as extension methods. (examples)
- For
IEquatable<T>
type:ThrowIfEqualTo()
ThrowIfNotEqualTo()
- For
IComparable<T>
type:ThrowIfLessThan()
ThrowIfLessThanOrEqualTo()
ThrowIfGreaterThan
ThrowIfGreaterThanOrEqualTo()
- For numeric types:
System.SByte
,System.Int16
,System.Int32
,System.Int64
,
System.Byte
,System.UInt16
,System.UInt32
,System.UInt64
,
System.Half
,System.Single
,System.Double
,System.Decimal
ThrowIfZero()
ThrowIfNegative()
ThrowIfNegativeOrZero()
ThrowIfPositive()
ThrowIfPositiveOrZero()
The exception messages are in English but they can be configured.
using EgonsoftHU.Extensions.Bcl;
// This method is intended to be used for non-localizable error messages.
ErrorMessageConfiguration.Current.ConfigureErrorMessage(
errorMessageKey: ErrorMessageKey.ArgumentOutOfRange_MustBeNonZero,
errorMessage: "The parameter '{0}' must be non-zero. Actual value: {1}"
);
// This method is intended to be used for localizable error messages.
ErrorMessageConfiguration.Current.ConfigureErrorMessage(
errorMessageKey: ErrorMessageKey.ArgumentOutOfRange_MustBeNonZero,
errorMessageResourceType: typeof(YourCustomValidationResources),
// This parameter is optional. If not specified then the value of errorMessageKey is used.
errorMessageResourceName: "YourCustomResourceName"
);
This package contains extension methods for these types:
System.Collections.Generic.ICollection<T>
AddRange()
System.Collections.Generic.IDictionary<TKey, TValue>
AsReadOnly()
AsSorted()
DefaultIfKeyNotFound()
GetOrThrow()
System.Collections.Generic.IEnumerable<T>
IsNullOrEmpty()
DetectChanges()
- Get new, existing and removed items.
System.Collections.Generic.IList<T>
AsReadOnly()
up to .NET 6 (An extension method with the same signature is part of .NET 7+.)
System.DateOnly
IsInRange()
System.DateTime
IsInRange()
ToMinutePrecision()
ToSecondPrecision()
System.DateTimeOffset
IsInRange()
ToMinutePrecision()
ToSecondPrecision()
System.Exception
GetHttpStatusCode()
SetHttpStatusCode()
System.IO.Stream
ToByteArray()
ToByteArrayAsync()
TryResetStreamPosition()
System.Reflection.Assembly
System.Reflection.MemberInfo
System.Reflection.ParameterInfo
System.String
IsNullOrEmpty()
IsNullOrWhiteSpace()
DefaultIfNullOrEmpty()
DefaultIfNullOrWhiteSpace()
EnsureTrailingSlash()
EnsureNoTrailingSlash()
System.Type
AsNullableValueType()
IsNullableValueType()
- Numeric types:
System.SByte
,System.Int16
,System.Int32
,System.Int64
,
System.Byte
,System.UInt16
,System.UInt32
,System.UInt64
,
System.Half
,System.Single
,System.Double
,System.Decimal
IsZero()
IsNegative()
IsNegativeOrZero()
IsPositive()
IsPositiveOrZero()
IsInRange()
IEnumerable
-relatedAsSingleElementSequence()
IsIn()
IsNotIn()
Reflection
-relatedGetPropertyValue()
TryPropertyValue()
SetPropertyValue()
TrySetPropertyValue()
- value selectors
GetStringValueOrNull()
GetStringValueOrEmptyString()
GetValueOrDefault()
GetValue()
Chars
DateTimeFormats
GenericTypeDefinitions
GuidFormats
(examples)HttpStatusCodes
Strings
IntervalBoundsOptions
- can be used e.g. in the newIsInRange()
extension methods.
EnumInfo<TEnum>
- A wrapper class that provides additional information about an
enum
type or value.- The custom attributes applied to a member.
- Easy access to the list of the defined members.
- Easy access to the name or the underlying value of a member.
- Support for customizing the serialized value through attributes.
- Implements bitwise / comparison / implicit conversion / equality operators.
- A wrapper class that provides additional information about an
EncodingProvider
UTF8WithoutBOM
The UTF-8 encoding without the Unicode byte order mark.
StringSyntaxAttribute
- up to .NET 6 (This attribute is part of .NET 7+.)
StructuralEqualityComparer<T>
- Provides a generic
IEqualityComparer<T>
instance for using the non-genericSystem.Collections.StructuralComparisons.StructuralEqualityComparer
.
- Provides a generic
TypeHelper
GetName<T>()
/GetName(Type)
methods as a shortcut forType.FullName ?? Type.Name
expression.