Dotnet Extensions
A small, focused set of C# extension methods that make every day .NET work more pleasant. Covers strings, enums, collections, objects, dates, comparisons, and exceptions. Lightweight, dependency-free, fully unit-tested.
- Package Id:
ArturRios.Extensions
Install
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/ArturRios/dotnet-extensions lib/ArturRios.Extensions
Then reference the project from your solution:
<ProjectReference Include="lib/ArturRios.Extensions/src/ArturRios.Extensions.csproj"/>
Supported frameworks
- .NET 10.0 (net10.0)
The library is built for modern .NET and has no external runtime dependencies.
Quick start
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 = "user@example.com".IsValidEmail(); // true
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
API overview
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.
String extensions (StringExtensions)
HasLowerChar()/HasUpperChar()/HasNumber()- True/false checks for at least one lower/upper/digit character.
HasMinLength(int min)/HasMaxLength(int max)- Length guardrails that treat
nullas failing and empty as length0.
- Length guardrails that treat
IsValidEmail()- Practical email pattern validation.
TrimChar(char c)- Trims the given character from both ends. Safe with
null.
- Trims the given character from both ends. Safe with
ParseToBoolOrDefault(bool default)/ParseToIntOrDefault(int default)- Safe parsing that returns the provided default on invalid or
nullinput.
- Safe parsing that returns the provided default on invalid or
ParseToObjectOrDefault<T>()- Parses JSON into
T; returnsnullon invalid or empty input.
- Parses JSON into
IsValidEnumValue<TEnum>(bool ignoreCase = true)- Checks if a string matches an enum name. Case-insensitive by default.
JoinWith(string separator = ", ")(forIEnumerable<string>andIEnumerable<object?>)- Concatenates elements with a separator, converting objects via
ToString()and allowingnull.
- Concatenates elements with a separator, converting objects via
Enumerable extensions (EnumerableExtensions)
IsEmpty()/IsNotEmpty()- Works for any
IEnumerable. Avoids materializing where possible.
- Works for any
PrintContents()- Writes primitive items directly and complex object properties to
Console.Out. Handlesnullenumerable.
- Writes primitive items directly and complex object properties to
Enum extensions (EnumExtensions)
GetDescription()- Returns the
DescriptionAttributevalue for an enum member, ornullwhen absent.
- Returns the
Object extensions (ObjectExtensions)
ToJsonStringContent()- Creates an
HttpContent(application/json) from an object usingSystem.Text.Json.
- Creates an
NonNullPropertiesToDictionary()/PropertiesToDictionary()- Reflects an object into a dictionary of property names to values, optionally skipping
nullvalues.
- Reflects an object into a dictionary of property names to values, optionally skipping
Generic extensions (GenericExtensions)
Clone<T>()- Safe deep clone for reference types, and value copy for value types. Returns
nullwhen the source isnull.
- Safe deep clone for reference types, and value copy for value types. Returns
DateTime extensions (DateTimeExtensions)
RemoveMilliseconds()- Drops milliseconds while preserving the
DateTimeKindand all other components.
- Drops milliseconds while preserving the
Comparison extensions (ComparisonExtensions)
In<T>(IEnumerable<T> set)/NotIn<T>(IEnumerable<T> set)- Membership helpers for readability.
Exception extensions (ExceptionExtensions)
ToLogLine(out Guid traceId)- Produces a single-line log string with timestamp, trace id, exception type, message, and stack trace. Throws
NullReferenceExceptionwhen called onnull.
- Produces a single-line log string with timestamp, trace id, exception type, message, and stack trace. Throws
Usage notes
- All methods are
staticextensions under theArturRios.Extensionsnamespace. - The library prioritizes clarity and safety: methods are null-aware where it makes sense and avoid throwing on common invalid input.
- No external dependencies; uses BCL APIs like
System.Text.Jsonand reflection where applicable.
Contributing
- Issues and PRs are welcome. If you plan a larger change, open an issue first with a short proposal.
- Coding style: follow existing conventions; keep APIs small and focused.
Build, test and publish
Use the official .NET CLI to build, test and publish the project and Git for source control. If you want, optional helper toolsets I built to facilitate these tasks are available:
Versioning
Semantic Versioning (SemVer). Breaking changes result in a new major version. New methods or non-breaking behavior changes increment the minor version; fixes or tweaks increment the patch.
Legal Details
This project is licensed under the MIT License. A copy of the license is available at LICENSE in the repository.