Base Class Library Enhancements
Randomization
Related class: RandomExtensions
Extension methods for random data generation extends both classes Random and RandomNumberGenerator.
Random string generation
Provides a way to generate random string of the given length and set of allowed characters.
using DotNext;
using System;
var rand = new Random();
var password = rand.NextString("abc123", 10);
//now password has 10 characters
//each character is equal to 'a', 'b', 'c', '1', '2' or '3'
The same extension method is provided for class RandomNumberGenerator.
Random boolean generation
Provides a way to generate boolean value with the given probability
using DotNext;
var rand = new Random();
var b = rand.NextBoolean(0.3D); //0.3 is a probability of TRUE value
The same extension method is provided for class RandomNumberGenerator.
String extensions
Related class: StringExtensions.
Reverse string
Extension method Reverse allows to reverse string characters and returns a new string:
using DotNext;
var str = "abc".Reverse(); //str is "cba"
Trim by length
Extension method TrimLength limits the length of string or span:
using DotNext;
var str = "abc".TrimLength(2); //str is "ab"
str = "abc".TrimLength(4); //str is "abc"
Span<int> array = new int[] { 10, 20, 30 };
array = array.TrimLength(2); //array is { 10, 20 }
Delegates
Related classes: DelegateHelpers, Func, Converter, Predicate.
Change type of delegate
Different types of delegates can refer to the same method. For instance, Func<string, string>
represents the same signature as Converter<string, string>
. It means that the delegate instance can be converted into another delegate type if signature matches.
using DotNext;
Func<string, int> lengthOf = str => str.Length;
Converter<string, int> lengthOf2 = lengthOf.ChangeType<Converter<string, int>>();
Specialized delegate converters
Conversion between mostly used delegate types: Predicate<T>, Func<T, TResult> and Converter<TInput, TOutput>.
using DotNext;
Predicate<string> isEmpty = str => str.Length == 0;
Func<string, bool> isEmptyFunc = isEmpty.AsFunc();
Converter<string, bool> isEmptyConv = isEmpty.AsConverter();
Predefined delegates
Cached delegate instances for mostly used delegate types: Predicate<T>, Func<T, TResult> and Converter<TInput, TOutput>.
using DotNext;
Func<int, int> identity = Func.Identity<int>(); //identity delegate which returns input argument without any changes
Predicate<string> truePredicate = Predicate.True<string>(); // predicate which always returns true
Predicate<object> falsePredicate = Predicate.False<string>(); //predicate which always returns false
Predicate<string> nullCheck = Predicate.IsNull<string>(); //predicate checking whether the input argument is null
Predicate<string> notNullCheck = Predicate.IsNotNull<string>(); //predicate checking whether the input argument is not null
Logical operators for Predicate
Extension methods implementation logical operators for Predicate delegate instances:
using DotNext;
Predicate<string> predicate = str => str.Length == 0;
predicate = predicate.Negate();
predicate = predicate.And(str => str.Length > 2);
predicate = predicate.Or(str => str.Length % 2 == 0);
predicate = predicate.Xor(Predicate.IsNull<string>());
Delegate Factories
C# 9 introduces typed function pointers. However, conversion between regular delegates and function pointers is not supported. DelegateHelpers
offers factory methods allowing creation of delegate instances from function pointers. These factory methods support implicit capturing of the first argument as well:
using DotNext;
static int GetHashCode(string s)
{
}
delegate*<string, int> hashCode = &GetHashCode;
Func<string, int> openDelegate = DelegateHelpers.CreateDelegate<string, int>(hashCode);
Func<int> closedDelegate = DelegateHelpers.CreateDelegate<string, int>(hashCode, "Hello, world!");
Comparable data types
Related class: Comparison
Range check
Checks whether the given value is in specific range.
using DotNext;
var b = 10.Between(5, 11, BoundType.Closed); //b == true
b = 10.Between(0, 4); //b == false
var i = 5.Clamp(4, 10); //i == 5
i = 5.Clamp(6, 10); //i == 6
i = 5.Clamp(0, 4); //i == 4
Equality check
Related classes: ObjectExtensions, ValueTypeExtensions.
Extension method IsOneOf allows to check whether the value is equal to one of the given values.
using DotNext;
var b = 42.IsOneOf(0, 5, 42, 3); //b == true
b = "a".IsOneOf("b", "c", "d"); //b == false
Array extensions
Related classes: OneDimensionalArray.
Extension methods for slicing, iteration, conversion, element insertion and fast equality check between elements of two arrays.
Equality check
BitwiseEquals extension method performs bitwise equality between two regions of memory referenced by the arrays. Element type of these arrays should be of unmanaged value type, e.g. int
, long
, System.Guid
.
var array2 = new int[] { 1, 2, 3 };
array2.BitwiseEquals(new [] {1, 2, 4}); //false
This method is faster than naive implementation using foreach
iteration and comparison by index. Read Benchmarks for more information.
Functional iteration
Extension method ForEach
allows to iterate over array elements and, optionally, modify array element.
var array = new string[] {"ab", "bc" };
array.ForEach((long index, ref string element) => {
if(element == "ab")
element = "";
});
Insertion and Removal
Extension methods Insert, Slice, RemoveLast and RemoveFirst allow to return modified array according with semantics of chosen method:
var array = new string[] {"a", "b"};
array = array.Insert("c", 2); //array == new []{"a", "b", "c"}
array = array.RemoveLast(2); //array == new []{"a"}
array = new string[] {"a", "b", "c"};
array = array.RemoveFirst(2); //array == new []{"c"}
array = new string[]{"a", "b", "c", "d"};
array = array.Slice(1, 2); //array == new []{"b", "c"}
The same behavior can be achieved using Span data type. However, these methods support large arrays where index and length cannot be represented by int data type.
Timestamp
Timestamp value type can be used as allocation-free alternative to Stopwatch when you need to measure time intervals.
using DotNext.Diagnostics;
using System;
var timestamp = Timestamp.Current;
//long-running operation
Console.WriteLine(current.Elapsed);
Elapsed
property returning value of TimeSpan type which indicates the difference between timestamp
and the current point in time.
This type should not be used as unique identifier of some point in time. The created time stamp may identify the time since the start of the process, OS, user session or whatever else.
Dynamic Task Result
In .NET it is not possible to obtain a result from task if its result type is not known at compile-time. It can be useful if you are writing proxy or SOAP Middleware using ASP.NET Core and task type is not known for your code. .NEXT provides two ways of doing that:
- Synchronous method
GetResult
which is located in Synchronization class - Asynchronous method
AsDynamic
which is located in Conversion class.
All you need is to have instance of non-generic Task class because all types tasks derive from it.
Under the hood, .NEXT uses Dynamic Language Runtime feature in combination with high-speed optimizations.
The following example demonstrates this approach:
using DotNext.Threading.Tasks;
using System.Threading.Tasks;
//assume that t is of unknown Task<T> type
Task t = Task.FromResult("Hello, world!");
//obtain result synchronously
Result<dynamic> result = t.GetResult(CancellationToken.None);
//obtain result asynchronously
dynamic result = await t.AsDynamic();
Hex Converter
BitConverter.ToString method from .NET standard library allow to convert array of bytes to its hexadecimal representation. However, it doesn't support Span<byte>
data type and therefore cannot be used in situations when bytes come from different source such as stack-allocated memory or segment of another array. Moreover, the method cannot place its result to variable of Span<char>
type and allocates new string every time. It may be unacceptable in low-latency scenario when number of memory allocations should be reduced.
Span static class exposes two static methods for fast hexadecimal conversion:
ToHex
allows to convertReadOnlySpan<byte>
to hexadecimal representation and places result toSpan<char>
FromHex
allows to convert hexadecimal string in the from ofReadOnlySpan<char>
to bytes and places result toSpan<byte>
using DotNext;
ReadOnlySpan<byte> bytes = stackalloc byte[] {8, 16, 24};
Span<char> hex = new stackalloc[bytes.Length * 2];
Span.ToHex(bytes, hex); //now hex == 081018
Polling of Concurrent Collections
IProducerConsumerCollection<T> is a common interface for concurrent collections in .NET library. Consumer of such collection uses TryTake
or more specialized method provided by subclasses to obtain elements from the collection. For convenience, Sequence static class offers GetConsumer
extension method to obtain consuming enumerable collection over the elements in the concurrent collection so you can use classic foreach loop:
using DotNext.Collections.Concurrent;
using System.Collections.Concurrent;
var queue = new ConcurrentQueue<int>();
foreach (int item in queue.GetConsumer())
{
// collection enumerator will call .TryDequeue(out int result) for each iteration of this loop
}