Adds missing overloads and more convenient ways to improve usability and readability of existing .NET APIs.
A few examples:
Process.WaitForExit(int milliseconds)
requests a timeout as anint
, in number of milliseconds, whileTimeSpan
is the preferred way to express time intervals.- Some old but still frequently used methods were implemented as static before the concept of extension methods have ever existed. While they would be much more convenient to use as extension methods, they have never been upgraded for compatibility reasons. Like it
if (String.IsNullOrWhiteSpace(str))
is easier to use asif (str.IsNullOrWhiteSpace())
. - Some APIs had limited customizations, because they missed overloads. Like while we could specify
StringComparison
forstr.IndexOf(string, StringComparison)
, we couldn't forstr.Contains(string)
. - .NET has more and more focus on performance, but some APIs are missing to be conscious about performance. Like when
ToList()
orToArray()
don't know about count of elements, they have to resize their target storage over and over while they finally arrive at the final size, which is expensive. So, we provide an overload that accepts a hintToList(int)
for storage size. - APIs that are not implemented yet, but they clearly fit into the future of the framework, like reading the lines from a
TextReader
as anIAsyncEnumerable<string>
.
ReadOnlySpan<byte>
- helper
string ToBase64String()
: converts a span of bytes to a Base 64 encoded string.
- helper
byte[]
- helper
MemoryStream ToMemoryStream()
: constructs a MemoryStream from an array of bytes.
- helper
DateTime
andDateTimeOffset
- new api
DateTimeOffset Floor(TimeSpan unit)
: rounds down a date and time to a specific time interval. - new api
DateTimeOffset Round(TimeSpan unit)
: rounds up or down a date and time to the nearest specific time interval. - new api
DateTimeOffset Ceiling(TimeSpan unit)
: rounds up a date and time to a specific time interval. - new api
DateTimeOffset StartOfWeek(DayOfWeek firstDayOfWeek)
: calculates the start of the week. - new api
string ToRfc3339Format()
: formats aDateTimeOffset
as RFC-3339 format. - helper
DateTimeOffset ToTimeZone(TimeZoneInfo destinationTimeZone)
: converts a date and time to another time zone.
- new api
Guid
- helper
Guid? TreatEmptyAsNull()
: returnsnull
when aGuid
isGuid.Empty
.
- helper
String
- helper
bool IsEmpty()
: determines whether the length of astring
is0
. - helper
bool IsNullOrEmpty()
: shortcut extension method forString.IsNullOrEmpty
. - helper
bool IsNullOrWhiteSpace()
: shortcut extension method forString.IsNullOrWhiteSpace
. - helper
bool IsWhiteSpace()
: determines whether astring
contains only whitespace characters. - helper
string? TreatEmptyAsNull()
: returnsnull
when astring
is empty. - helper
string? TreatWhiteSpaceAsNull()
: returnsnull
when astring
contains only whitespace. - performance
StringSegment Subsegment(int start, int length)
: constructs aStringSegment
from a part of astring
. - performance
StringSegment Subsegment(Range range)
: constructs aStringSegment
from a part of astring
. - new api
string Ellipsis(int maxLength, string ellipsis)
: trims astring
to a specific length and appendsellipsis
when it overflows. - new api
string TrimPrefix(string prefix)
: trims a specificstring
from the beginning of astring
. - new api
string TrimSuffix(string suffix)
: trims a specificstring
from the end of astring
. - helper
Uri ToUri(UriKind kind)
: constructs aUri
from astring
. - new api
string Remove(string subject, StringComparison comparison)
: removes all occurrences of a substring from a string. - new api
string Remove(string subject)
: removes all occurrences of a substring from a string.
- helper
StringSegment
- new api
IEnumerable<char> AsEnumerable()
: enumerates the characters of theStringSegment
. - missing overload
StringSegment TrimStart(char ch)
: overload with specificchar
to trim. - missing overload
StringSegment TrimStart(params char[] characters)
: overload with specificchar
s to trim. - missing overload
StringSegment TrimEnd(char ch)
: overload with specificchar
to trim. - missing overload
StringSegment TrimEnd(params char[] characters)
: overload with specificchar
s to trim. - missing overload
StringSegment Contains(char ch)
: overload withchar
. - missing overload
StringSegment StartsWith(char ch)
: overload withchar
. - missing overload
StringSegment EndsWith(char ch)
: overload withchar
. - missing overload
int LastIndexOfAny(int startIndex, params char[] characters)
: overload withchar[]
. - missing overload
int LastIndexOfAny(params char[] characters)
: overload withchar[]
. - missing overload
StringTokenizer Split(char ch)
: overload withchar
.
- new api
Uri
char
IEnumerable<T>
- new api
bool IsEmpty<T>()
: determines whether the enumerable is empty. - performance
IList<T> ToList<T>(int initialCapacity)
: provides a hint for the size of the list to be constructed, so the list doesn't have to be resized multiple times during the copy. - performance
T[] ToArray<T>(int sizeHint)
: provides a hint for the size of the array to be constructed, so the list doesn't have to be resized multiple times during the copy. - new api
bool TryFirst(out T element)
: determines whether the enumerable consists of at least one element, and if yes, gets the first element. - new api
bool TrySingle(out T element)
: determines whether the enumerable consists of a single element, and if yes, gets the element. - new api
bool TryLast(out T element)
: determines whether the enumerable consists of at least one element, and if yes, gets the last element.
- new api
ICollection<T>
IList<T>
- helper
IReadOnlyList<T> AsReadOnly<T>()
- new api
bool CanMoveUp<T>(T element)
: determines whether an element can be moved to the next index in a list. - new api
bool CanMoveDown<T>(T element)
: determines whether an element can be moved to the previous index in a list. - new api
void MoveUp<T>(T element)
: moves an element to the next index in a list. - new api
void MoveDown<T>(T element)
: moves an element to the previous index in a list.
- helper
IReadOnlyList<T>
- new api
int IndexOf<T>(Predicate<T> predicate, int startIndex)
- new api
int IndexOf<T>(Predicate<T> predicate)
- new api
int LastIndexOf<T>(Predicate<T> predicate, int startIndex)
- new api
int LastIndexOf<T>(Predicate<T> predicate)
- new api
int IndexOf<T>(T element, int startIndex, IEqualityComparer<T> comparer)
- new api
int IndexOf<T>(T element)
: finds the position of an element in a list, starting from the beginning, using the default comparer. - new api
int LastIndexOf<T>(T element, int startIndex, IEqualityComparer<T> comparer)
- new api
int LastIndexOf<T>(T element)
: finds the last position of an element in a list, starting from the beginning, using the default comparer.
- new api
IDictionary<TKey, TValue>
- helper
IReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>()
: converts a dictionary to read-only. - helper
SortedDictionary<TKey, TValue> AsSorted<TKey, TValue>()
: converts a dictionary to aSortedDictionary
. - helper
void AddRange<TKey, TValue>(IEnumerable<KeyVaulePair<TKey, TValue>> elements)
: adds a range of elements to a dictionary.
- helper
IEnumerator<T>
- new api
bool TryMoveNext<T>(out T current)
: wrapsMoveNext()
andCurrent
to a Try-pattern.
- new api
Stream
- new api
Stream Rewind()
: sets its position to0
. - new api
Task<MemoryStream> BufferAsync(int bufferSize, CancellationToken cancellationToken)
: copies the contents of a stream into aMemoryStream
. - new api
Task<byte[]> ToByteArrayAsync(CancellationToken cancellationToken)
: copies the contents of a stream into a byte array.
- new api
TextReader
- new api
IAsyncEnumerable<string> EnumerateLinesAsync()
: reads aTextReader
line by line, asynchronously.
- new api
Process
- missing overload
bool WaitForExit(TimeSpan timeout)
: provides an overload withTimeSpan
instead ofint milliseconds
.
- missing overload
IPAddress
RandomNumberGenerator
- missing overload
int GetInt32()
: gets a random integer.
- missing overload
PropertyInfo
FieldInfo