Changelog
Notable changes in each release.
A small, focused set of C# extension methods that make every day .NET work more pleasant. Covers strings, enums, collections, objects, dates, numbers, comparisons, and exceptions. Lightweight and fully unit-tested.
NuGet:
# Package Manager
Install-Package ArturRios.Extensions
# .NET CLI
dotnet add package ArturRios.Extensions
Git submodule:
# add under ./lib/ArturRios.Extensions
git submodule add https://github.com/artur-rios/dotnet-extensions lib/ArturRios.Extensions
Then reference the project from your solution:
<ProjectReference Include="lib/ArturRios.Extensions/src/ArturRios.Extensions.csproj"/>
A couple of small NuGet packages are pulled in transitively:
Clone<T>().Add a using to bring extensions into scope and call them like any other instance method:
using ArturRios.Extensions;
var hasLower = "AbC".HasLowerChar(); // true
var hasUpper = "abc".HasUpperChar(); // false
var hasNumber = "a1b".HasNumber(); // true
var okMin = "ab".HasMinLength(2); // true
var okMax = "abcd".HasMaxLength(3); // false
var isEmail = "[email protected]".IsValidEmail(); // true
var idn = "user@héllo.com".IsValidEmail(); // true - the domain is punycoded first
var cleaned = "xxvaluexx".TrimChar('x'); // "value"
var value = ((string?)null).ValueOrDefault("fallback"); // "fallback"
var numbers = new[] { 1, 2, 3 };
var printed = numbers.JoinWith(" | "); // "1 | 2 | 3"
var inSet = 2.In(0, 2, 4); // true
var notIn = 3.NotIn(0, 2, 4); // true
var at = new DateTime(2025, 12, 10, 15, 30, 45, 999).RemoveMilliseconds();
// 2025-12-10 15:30:45.000
var prime = 7.IsPrime(); // true
Below is a summary of the most commonly used extension methods with short examples. See source files under src/ and
unit tests under tests/ for the full list and behavior.
StringExtensions)HasLowerChar() / HasUpperChar() / HasNumber()HasMinLength(int min) / HasMaxLength(int max)string; an empty string has length 0.IsValidEmail()ArturRios.Util.Text.EmailAddress.IsValid, so the domain is lowercased and punycoded
before the syntax check. Mixed-case and internationalized domains are accepted.TrimChar(char c)"- a -".TrimChar('-') is " a ". Empty input comes back unchanged.ValueOrDefault(string? defaultValue = null)null/empty as no value.ParseToBoolOrDefault(bool? defaultValue = null) / ParseToIntOrDefault(int? defaultValue = null)bool? / int? that returns the provided default — null unless you pass one — on invalid or
null input. Integers are parsed with the invariant culture, so "-5" is -5 whatever the machine’s culture.ParseToObjectOrDefault<T>()T; returns null on invalid or empty input. Property names match regardless of case, so
{"name":"Ana"} binds to a Name property.IsValidEnumValue<TEnum>(bool ignoreCase = true)"999" is rejected by a three-member enum.JoinWith(string separator = ", ") (for IEnumerable<string> and any IEnumerable<T>)ToString() and allowing null.EnumerableExtensions)IsEmpty() / IsNotEmpty()IEnumerable. Avoids materializing where possible.PrintContents()Console.Out. Handles null enumerable.
Enums, Guid, DateTime, DateTimeOffset, DateOnly, TimeOnly and TimeSpan items print as one value;
static properties and indexers are skipped.EnumExtensions)GetDescription()DescriptionAttribute value for an enum member, or null when absent.ObjectExtensions)NonNullPropertiesToDictionary() / PropertiesToDictionary()null values. Only
public, readable instance properties are included: static properties, indexers and write-only properties are
skipped.GenericExtensions)Clone<T>()null when the source is null, and throws when the graph
cannot be round-tripped — a reference cycle, for example. The clone is only as faithful as the round
trip: members the serializer ignores are absent, and reference identity within the graph is not kept.DateTimeExtensions)RemoveMilliseconds()DateTimeKind. Every sub-second component goes, not only
the milliseconds: microseconds and the remaining ticks go with them.IntExtensions)IsPrime()int, long, short, byte,
and the uint/ulong/ushort/sbyte variants).ComparisonExtensions)In<T>(params T[] range) / NotIn<T>(params T[] range)EqualityComparer<T>.Default, so two
nulls compare equal and a type’s own Equals / IEquatable<T> is honoured. An empty range never
matches; a null range throws ArgumentNullException.ExceptionExtensions)ToLogLine(out Guid traceId)\n, so one exception is always one line. Throws
NullReferenceException when called on null.static extensions under the ArturRios.Extensions namespace.string validation helpers, for instance — calling it on null is a compiler warning and a runtime
failure, not a silent false.System.Text.Json and reflection where applicable, plus a couple of small NuGet dependencies
(see Dependencies).This project is licensed under the Artur Rios Attribution License. It is free and open source: you may view, use, fork, modify, and extend it, provided you credit the author, Artur Rios, with a link to artur-rios.tech or github.com/artur-rios.
Notable changes in each release.
Building from source, running the tests, branching and releasing.