Diamond.Utility 2026.0.0

dotnet add package Diamond.Utility --version 2026.0.0
                    
NuGet\Install-Package Diamond.Utility -Version 2026.0.0
                    
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="Diamond.Utility" Version="2026.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Diamond.Utility" Version="2026.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Diamond.Utility" />
                    
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 Diamond.Utility --version 2026.0.0
                    
#r "nuget: Diamond.Utility, 2026.0.0"
                    
#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 Diamond.Utility@2026.0.0
                    
#: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=Diamond.Utility&version=2026.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Diamond.Utility&version=2026.0.0
                    
Install as a Cake Tool

Diamond.Utility Package

Diamond.Utility is a comprehensive, lightweight library of utilities, helper classes, and extended functionalities for the .NET ecosystem. It is designed for use in Web, WPF, Windows Services, Console and any other .NET application type.

  • Key Features

  • Framework-independent (compatible with .NET Standard 2.0+)

  • Zero third-party dependencies

  • Memory-efficient and performance-optimized

  • Suitable for multi-layered and modern architectures


Some Components

1. WeakReference Collections

These collections use WeakReference to store object references, preventing memory leaks and allowing proper garbage collection. Ideal for caching, event handling, and memory-sensitive scenarios.

WeakReferenceDictionary<TKey, TValue>

A dictionary that stores values as weak references.

var cache = new WeakReferenceDictionary<string, MyClass>();
cache["key"] = new MyClass();
var value = cache["key"]; // May return null if GC has collected it
WeakReferenceCollection<T>

A collection of objects with weak references.

var list = new WeakReferenceCollection<MyClass>();
list.Add(new MyClass());
foreach (var item in list)
{
    // item may be null if garbage collected
}

2. Dynamic Values with HotValue

HotValue is an intelligent data type that stores a Func<T> instead of the actual value. This ensures your values are always up-to-date and reflect changes from the source immediately.

Basic Example:
var hotNow = new Func<DateTime>(() => DateTime.Now);
var hotNow2000 = HotValue.Min(hotNow, HotValue.Const(new DateTime(2000, 1, 1)));
var hotNowCache = HotValue.Cache(hotNow2000, TimeSpan.FromSeconds(30));

Console.WriteLine(hotNow()); // Current time
Console.WriteLine(hotNowCache()); // Current time
Thread.Sleep(5000);
Console.WriteLine(hotNow()); // Updated time
Console.WriteLine(hotNowCache()); // Cached time
var c = new CacheSingleTime<string, string>();
string? v1 = c["key"]; //null
Func<string?> alwaysUpdateValue = c.GetHotValue("key");

3. Lazy Caching - Diamond.LazyCache

Combines Lazy Loading and Caching concepts, allowing you to cache data and easily mark it as Dirty to regenerate on next access.

var cache = LazyCache.Create(() => DateTime.Now, TimeSpan.FromSeconds(30));

Thread.Sleep(5000);

// First access: LoadExpensiveDataFromDatabase executes
Console.WriteLine(cache.Value); // Current time

Thread.Sleep(5000);
// Second access: Returns cached value (no function execution)
Console.WriteLine(cache.Value); // Cached time

// Mark cache as dirty
cache.SetDirty();

Thread.Sleep(5000);

// Next access: Function executes again
Console.WriteLine(cache.Value); // Current time

4. Stream Utilities

StreamDecorator

A base class for implementing the Decorator Pattern with streams, providing easy inheritance and stream decoration.

UnionStream

Concatenates multiple streams into a single ReadOnly stream. Perfect when reading from multiple data sources sequentially.

var stream1 = File.OpenRead("file1.bin");
var stream2 = File.OpenRead("file2.bin");
var stream3 = new MemoryStream(data);

using var union = new UnionStream(stream1, stream2, stream3);
// All three streams are read sequentially
Stream mainForwardStream = ...
var buffer = new byte[1024];
mainForwardStream.Read(buffer);

// any proccess

var header = new MemoryStream(buffer);

using var union = new UnionStream(header, mainForwardStream);
// All two streams are read sequentially
MultiProcessWriterStream

Enables multiple independent processes to write to the same file. Ideal for inter-process logging scenarios.

using var writer = MultiProcessWriterStream.Create("log.txt");
using var sw = new StreamWriter(writer);
sw.WriteLine($"[{DateTime.Now}] Process {Process.GetCurrentProcess().Id} wrote this");

5. LINQ Extensions

Powerful extension methods for IQueryable and IEnumerable:

HasOverlap

Check if two range overlap:

DateTime? dt1 = new DateTime(2020, 1, 1);
DateTime? dt2 = new DateTime(2020, 5, 1);
var query = dbContext.Rules.HasOverlap(r => r.BeginDate, r => r.EndDate, dt1, dt2);
var list = query.ToList();
SelectPage

Easy pagination with comprehensive result:

var paging = dbContext.Products.Where(r => r.Price > 1000).SelectPage();
paging.PageSize = 40;
paging.ExpectedPageIndex = 1;
paging.OrderKey(r => r.Price);

// Returns: DataSource, AllCount, PageCount, ActualPageIndex, ...

6. Observer Pattern Implementation - ConcurrentObservable<T>

A thread-safe implementation of IObserver and IObservable interfaces:

var bus = new ConcurrentObservable<int>();

// Subscribe
var subscription = bus.Observable.Subscribe(value => 
{
    Console.WriteLine($"Received: {value}");
});

// Publish data
bus.Observers.OnNext(42);

// Unsubscribe
subscription.Dispose();

7. Text Normalization - Normalizer

A fast and comprehensive class for text cleaning and normalization with:

  • Persian/Arabic character unification (normalizing ی, ک, ة, etc.)
  • Number normalization (converting Arabic/Persian digits to English or vice versa)
  • Whitespace normalization (removing extra spaces, unifying whitespace types)
  • Control character removal
var n = new Normalizer();
n.LineOption = Normalizer.LineOptions.MultiLine_CRLF_Windows;
n.TrimOption = Normalizer.TrimOptions.AnyLine;
n.Add(Normalizer.Persian());
n.Add(Normalizer.ReplaceDigits(Normalizer.DigitCulture.Ascii));
n.Add(Normalizer.ReplaceWhiteSpace(true, ' ', true));
n.Add(Normalizer.RemoveDuplicate(" "));
n.Add(Normalizer.ToLower());
n.Add(Normalizer.RemoveCategory(System.Globalization.UnicodeCategory.Control));

var corrector = n.CreateTextCorrector();

string text = "Abc\t\t 1௨൩٤۵ ﻙﻱ";
string normalized = corrector.Correct(text, 0, int.MaxValue);
// Output: "abc 12345 کی"

8. Dynamic Text Search - Text.Search

Static class for tokenizing search phrases and converting them to Expression<Func<T, bool>> for use with IQueryable:

var searchTerm = "book +c# -'java script'";
var tokens = Search.GetParts(searchTerm);

// Build filter for multiple fields
Expression<Func<Product, bool>>? filter = Search.GetExpression<Product>(
    tokens,
    p => p.Name,
    p => p.Description,
    p => p.Brand
);

var results = dbContext.Products.Where(filter).ToList();

9. Persian Date Parsing - PersianDateParse

Static function for parsing Persian (Shamsi/Hijri) dates in various text and numeric formats:

// Supports multiple input formats
DateTime? date1 = DateParser.PersianDateParse("1403 /04/15");
DateTime? date2 = DateParser.PersianDateParse("1403-04- 15");
DateTime? date3 = DateParser.PersianDateParse("14030415");
DateTime? date4 = DateParser.PersianDateParse("15. 04-1403");
DateTime? date5 = DateParser.PersianDateParse("۱۵ تیر ۱۴۰۳");
DateTime? date6 = DateParser.PersianDateParse("1403/4/15 14:30:00");

10. Additional Utilities

Diamond.Text.StringParse.TryParse Diamond.Text.StringParse.ToHex Diamond.Text.StringParse.FromHex Diamond.Text.LikeOperator.Like Diamond.Collections.Generic.Functions.GetNameValue Diamond.Collections.Generic.IFeatureSet Diamond.Net.Functions.GetIPScope Diamond.IReferenceTracker Diamond.PlatformRuntimeEnvironment etc...


License

Apache-2.0 license

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Diamond.Utility:

Package Downloads
Diamond.Addons

Addons engine library Samples: https://github.com/Hajisharifi/Diamond-Samples/tree/master/Addons

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.0.0 0 7/2/2026
2025.0.0 2,175 7/19/2025
2022.0.0 2,734 4/15/2022
2021.5.0 2,455 6/8/2021
2021.2.0 3,499 3/12/2021
2020.11.0 2,499 11/23/2020
2020.10.0 2,442 10/25/2020
2020.1.0 2,542 5/15/2020
2020.0.0 3,267 5/12/2020
2019.12.1 2,555 1/3/2020
2019.12.0 3,374 12/20/2019
2019.8.0 2,537 9/7/2019
2019.3.0 2,549 7/19/2019
2019.0.0 4,264 6/7/2019
2018.7.3 2,997 8/5/2018
2018.7.0 5,580 7/15/2018