AbYzzX.System.Extensions 0.0.7.1

dotnet add package AbYzzX.System.Extensions --version 0.0.7.1
                    
NuGet\Install-Package AbYzzX.System.Extensions -Version 0.0.7.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AbYzzX.System.Extensions" Version="0.0.7.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AbYzzX.System.Extensions" Version="0.0.7.1" />
                    
Directory.Packages.props
<PackageReference Include="AbYzzX.System.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AbYzzX.System.Extensions --version 0.0.7.1
                    
#r "nuget: AbYzzX.System.Extensions, 0.0.7.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AbYzzX.System.Extensions@0.0.7.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AbYzzX.System.Extensions&version=0.0.7.1
                    
Install as a Cake Addin
#tool nuget:?package=AbYzzX.System.Extensions&version=0.0.7.1
                    
Install as a Cake Tool

System.Extensions

A collection of useful extension methods and utility classes for .NET applications that enhance the standard System namespace with commonly needed functionality.

Overview

System.Extensions provides a set of carefully crafted extension methods and helper classes that simplify common programming tasks in .NET. The library focuses on argument validation, type operations, and collection manipulation.

Features

  • Argument Validation: Comprehensive Check utility class for parameter validation
  • Type Extensions: Enhanced type checking and reflection utilities
  • Collection Extensions: Useful extension methods for working with collections
  • Enumerable Extensions: Recursive flattening and tree traversal support
  • Dictionary Extensions: Additional dictionary manipulation methods

Installation

dotnet add package System.Extensions

Components

Check Class

A robust argument validation utility with a fluent API that helps you write defensive code with minimal boilerplate.

Null Checking
public void ProcessUser(User user, string name)
{
    Check.NotNull(user, nameof(user));
    Check.NotNull(name, nameof(name));
    
    // Your logic here
}
String Validation
public void CreateUser(string username, string email)
{
    // Validate non-null or whitespace
    Check.NotNullOrWhiteSpace(username, nameof(username));
    
    // Validate with length constraints
    Check.NotNullOrEmpty(email, nameof(email), maxLength: 100, minLength: 5);
    
    // Length validation only
    Check.Length(email, nameof(email), maxLength: 100, minLength: 5);
}
Collection Validation
public void ProcessItems(ICollection<string> items)
{
    Check.NotNullOrEmpty(items, nameof(items));
    // items is guaranteed to be non-null and contain at least one element
}
Numeric Range Validation
public void SetAge(int age)
{
    Check.Positive(age, nameof(age));
    Check.Range(age, nameof(age), minimumValue: 0, maximumValue: 150);
}

public void SetPrice(decimal price)
{
    Check.Positive(price, nameof(price));
}
Type Validation
public void RegisterService(Type serviceType)
{
    Check.AssignableTo<IService>(serviceType, nameof(serviceType));
    // serviceType is guaranteed to implement IService
}
Struct Validation
public void ProcessDate(DateTime? date)
{
    var validDate = Check.NotDefaultOrNull(date, nameof(date));
    // validDate is guaranteed to be non-null and not default(DateTime)
}

Type Extensions

Enhanced type checking and reflection utilities.

Type Assignment Checking
Type myType = typeof(MyClass);

// Check if type is assignable to target type
if (myType.IsAssignableTo<IMyInterface>())
{
    // Type implements IMyInterface
}

if (myType.IsAssignableTo(typeof(BaseClass)))
{
    // Type inherits from BaseClass
}
Generic Type Checking
Type listType = typeof(List<string>);

if (listType.IsAssignableToGenericType(typeof(IEnumerable<>)))
{
    // listType implements IEnumerable<T>
}
Base Class Retrieval
Type myType = typeof(MyClass);

// Get all base classes
Type[] baseClasses = myType.GetBaseClasses(includeObject: false);

// Get base classes up to a specific type
Type[] baseClasses = myType.GetBaseClasses(typeof(BaseClass), includeObject: false);
Type Name Utilities
Type myType = typeof(MyClass);
string fullName = myType.GetFullNameWithAssemblyName();
// Returns: "MyNamespace.MyClass, MyAssembly"

Collection Extensions

Additional methods for working with collections.

// Example usage (specific methods depend on implementation)
var collection = new List<int> { 1, 2, 3 };
// Use collection extension methods

Dictionary Extensions

Enhanced dictionary manipulation methods.

// Example usage (specific methods depend on implementation)
var dictionary = new Dictionary<string, int>();
// Use dictionary extension methods

Enumerable Extensions

Powerful enumerable extensions for working with hierarchical data.

Flatten Method

Recursively flattens a tree-like structure into a flat enumerable.

public class TreeNode
{
    public string Name { get; set; }
    public List<TreeNode> Children { get; set; } = new();
}

var root = new TreeNode
{
    Name = "Root",
    Children = new List<TreeNode>
    {
        new() { Name = "Child 1", Children = new() { new() { Name = "Grandchild 1" } } },
        new() { Name = "Child 2" }
    }
};

// Flatten the tree structure
var allNodes = new[] { root }.Flatten(node => node.Children);
// Result: Root, Child 1, Grandchild 1, Child 2

foreach (var node in allNodes)
{
    Console.WriteLine(node.Name);
}
Use Cases for Flatten
// File system traversal
public class FileSystemItem
{
    public string Path { get; set; }
    public List<FileSystemItem> SubItems { get; set; }
}

var allFiles = rootFolder.Flatten(f => f.SubItems);

// Organization hierarchy
public class Employee
{
    public string Name { get; set; }
    public List<Employee> DirectReports { get; set; }
}

var allEmployees = ceo.Flatten(e => e.DirectReports);

// UI component tree
public class UIComponent
{
    public string Id { get; set; }
    public List<UIComponent> Children { get; set; }
}

var allComponents = rootComponent.Flatten(c => c.Children);

Complete Examples

API Controller with Validation

public class UserController
{
    public void CreateUser(CreateUserRequest request)
    {
        Check.NotNull(request, nameof(request));
        Check.NotNullOrWhiteSpace(request.Username, nameof(request.Username), maxLength: 50);
        Check.NotNullOrWhiteSpace(request.Email, nameof(request.Email), maxLength: 100);
        Check.Range(request.Age, nameof(request.Age), minimumValue: 18, maximumValue: 120);
        
        // Create user logic
    }
}

Service Registration with Type Checking

public void RegisterService<TService, TImplementation>()
    where TImplementation : TService
{
    var implementationType = typeof(TImplementation);
    var serviceType = typeof(TService);
    
    Check.NotNull(implementationType, nameof(implementationType));
    Check.AssignableTo<TService>(implementationType, nameof(implementationType));
    
    if (serviceType.IsAssignableToGenericType(typeof(IDisposable)))
    {
        // Register with disposal support
    }
    
    // Registration logic
}

Hierarchical Data Processing

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Category> SubCategories { get; set; } = new();
}

public IEnumerable<Category> GetAllCategories(Category rootCategory)
{
    Check.NotNull(rootCategory, nameof(rootCategory));
    
    return new[] { rootCategory }.Flatten(c => c.SubCategories);
}

public Category? FindCategory(Category root, int categoryId)
{
    return new[] { root }
        .Flatten(c => c.SubCategories)
        .FirstOrDefault(c => c.Id == categoryId);
}

Best Practices

Use Check for All Public Methods

public class MyService
{
    public void ProcessData(string data, IConfiguration config)
    {
        // Always validate public method parameters
        Check.NotNullOrWhiteSpace(data, nameof(data));
        Check.NotNull(config, nameof(config));
        
        // Your logic here
    }
    
    private void InternalMethod(string data)
    {
        // Internal methods may skip validation if callers are trusted
    }
}

Combine Multiple Validations

public void CreateProduct(string name, decimal price, int stock)
{
    Check.NotNullOrWhiteSpace(name, nameof(name), maxLength: 100, minLength: 3);
    Check.Positive(price, nameof(price));
    Check.Range(stock, nameof(stock), minimumValue: 0);
    
    // Create product
}

Use Type Extensions for Generic Code

public void ProcessGenericType(Type type)
{
    Check.NotNull(type, nameof(type));
    
    if (type.IsAssignableTo<IDisposable>())
    {
        // Handle disposable types
    }
    
    if (type.IsAssignableToGenericType(typeof(IEnumerable<>)))
    {
        // Handle enumerable types
    }
}

Requirements

  • .NET 9.0 or later
  • C# 13.0

Dependencies

This package has no external dependencies and only extends the System namespace.

License

This project is licensed under the terms specified in the project license file.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.


Built with ❤️ for the .NET community

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AbYzzX.System.Extensions:

Package Downloads
AbYzzX.Avalonia.Modular

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.7.1 203 11/27/2025
0.0.7 213 11/27/2025
0.0.6 204 11/26/2025
0.0.5 209 11/24/2025
0.0.4 175 11/22/2025
0.0.3 205 11/22/2025
0.0.2 255 11/22/2025
0.0.1 170 11/8/2025