Skip to content

Commit df452a7

Browse files
Converted some of the existing extension methods into .NET 10 extension blocks.
1 parent 9a7bb9b commit df452a7

File tree

5 files changed

+493
-486
lines changed

5 files changed

+493
-486
lines changed

OnixLabs.Core/Extensions.Array.cs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,45 @@ namespace OnixLabs.Core;
2525
public static class ArrayExtensions
2626
{
2727
/// <summary>
28-
/// Creates a copy of the current <see cref="T:T[]"/>.
28+
/// Provides extensions for generic arrays.
2929
/// </summary>
30-
/// <param name="array">The current <see cref="T:T[]"/> to copy.</param>
31-
/// <typeparam name="T">The underlying type of the array.</typeparam>
32-
/// <returns>Returns an exact copy of the current <see cref="T:T[]"/>.</returns>
33-
public static T[] Copy<T>(this T[] array) => [..array];
30+
/// <param name="receiver">The current generic array.</param>
31+
/// <typeparam name="T">The underlying element type of the current generic array.</typeparam>
32+
extension<T>(T[] receiver)
33+
{
34+
/// <summary>
35+
/// Creates a copy of the current <typeparamref name="T"/> array.
36+
/// </summary>
37+
/// <returns>Returns an exact copy of the current <typeparamref name="T"/> array.</returns>
38+
public T[] Copy() => [..receiver];
3439

35-
/// <summary>
36-
/// Creates a copy of the current <see cref="T:T[]"/>.
37-
/// </summary>
38-
/// <param name="array">The current <see cref="T:T[]"/> to copy.</param>
39-
/// <param name="index">The index of the array to begin copying from.</param>
40-
/// <param name="count">The number of elements of the array to copy.</param>
41-
/// <typeparam name="T">The underlying type of the array.</typeparam>
42-
/// <returns>Returns an exact copy of the current <see cref="T:T[]"/>.</returns>
43-
public static T[] Copy<T>(this T[] array, int index, int count) => [..array[index..(index + count)]];
40+
/// <summary>
41+
/// Creates a copy of the current <typeparamref name="T"/> array.
42+
/// </summary>
43+
/// <param name="index">The index of the array to begin copying from.</param>
44+
/// <param name="count">The number of elements of the array to copy.</param>
45+
/// <returns>Returns an exact copy of the current <typeparamref name="T"/> array.</returns>
46+
public T[] Copy(int index, int count) => [..receiver[index..(index + count)]];
4447

45-
/// <summary>
46-
/// Concatenates the current <see cref="T:T[]"/> with another <see cref="T:T[]"/>.
47-
/// </summary>
48-
/// <param name="array">The source <see cref="T:T[]"/> to concatenate with the other <see cref="T:T[]"/>.</param>
49-
/// <param name="other">The other <see cref="T:T[]"/> to concatenate with the source <see cref="T:T[]"/>.</param>
50-
/// <typeparam name="T">The underlying type of the <see cref="T:T[]"/>.</typeparam>
51-
/// <returns>Returns the current <see cref="T:T[]"/> concatenated with the other <see cref="T:T[]"/>.</returns>
52-
public static T[] ConcatenateWith<T>(this T[] array, T[] other) => [..array, ..other];
48+
/// <summary>
49+
/// Concatenates the current <typeparamref name="T"/> array with another <typeparamref name="T"/> array.
50+
/// </summary>
51+
/// <param name="other">The other <typeparamref name="T"/> array to concatenate with the current <typeparamref name="T"/> array.</param>
52+
/// <returns>Returns a new <typeparamref name="T"/> array, consisting of the current <typeparamref name="T"/> array concatenated with the other <typeparamref name="T"/> array.</returns>
53+
public T[] ConcatenateWith(T[] other) => [..receiver, ..other];
54+
}
5355

5456
/// <summary>
55-
/// Obtains the <see cref="string"/> representation of the current <see cref="T:Byte[]"/>.
57+
/// Provides extension methods for <see cref="byte"/> arrays.
5658
/// </summary>
57-
/// <param name="array">The <see cref="T:Byte[]"/> instance from which to obtain a <see cref="string"/> representation.</param>
58-
/// <param name="encoding">The <see cref="Encoding"/> which will be used to convert the current <see cref="T:Byte[]"/> into a <see cref="string"/> representation.</param>
59-
/// <returns>Returns the <see cref="string"/> representation of the current <see cref="T:Byte[]"/>.</returns>
60-
public static string ToString(this byte[] array, Encoding encoding) => encoding.GetString(array);
59+
/// <param name="array">The current <see cref="byte"/> array.</param>
60+
extension(byte[] array)
61+
{
62+
/// <summary>
63+
/// Obtains the <see cref="string"/> representation of the current <see cref="byte"/> array.
64+
/// </summary>
65+
/// <param name="encoding">The <see cref="Encoding"/> which will be used to convert the current <see cref="byte"/> array into a <see cref="string"/> representation.</param>
66+
/// <returns>Returns the <see cref="string"/> representation of the current <see cref="byte"/> array.</returns>
67+
public string ToString(Encoding encoding) => encoding.GetString(array);
68+
}
6169
}

OnixLabs.Core/Extensions.DateTime.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@ namespace OnixLabs.Core;
2424
public static class DateTimeExtensions
2525
{
2626
/// <summary>
27-
/// Obtains a <see cref="DateOnly"/> instance from the current <see cref="DateTime"/> instance.
27+
/// Provides extension methods for <see cref="DateTime"/> instances.
2828
/// </summary>
29-
/// <param name="value">The <see cref="DateTime"/> instance from which to obtain a <see cref="DateOnly"/> instance.</param>
30-
/// <returns>Returns the <see cref="DateOnly"/> instance obtained from the current <see cref="DateTime"/> instance.</returns>
31-
public static DateOnly ToDateOnly(this DateTime value) => DateOnly.FromDateTime(value);
29+
/// <param name="receiver">The current <see cref="DateTime"/> instance.</param>
30+
extension(DateTime receiver)
31+
{
32+
/// <summary>
33+
/// Obtains a <see cref="DateOnly"/> instance from the current <see cref="DateTime"/> instance.
34+
/// </summary>
35+
/// <returns>Returns the <see cref="DateOnly"/> instance obtained from the current <see cref="DateTime"/> instance.</returns>
36+
public DateOnly ToDateOnly() => DateOnly.FromDateTime(receiver);
3237

33-
/// <summary>
34-
/// Obtains a <see cref="TimeOnly"/> instance from the current <see cref="TimeOnly"/> instance.
35-
/// </summary>
36-
/// <param name="value">The <see cref="DateTime"/> instance from which to obtain a <see cref="TimeOnly"/> instance.</param>
37-
/// <returns>Returns the <see cref="TimeOnly"/> instance obtained from the current <see cref="DateTime"/> instance.</returns>
38-
public static TimeOnly ToTimeOnly(this DateTime value) => TimeOnly.FromDateTime(value);
38+
/// <summary>
39+
/// Obtains a <see cref="TimeOnly"/> instance from the current <see cref="TimeOnly"/> instance.
40+
/// </summary>
41+
/// <returns>Returns the <see cref="TimeOnly"/> instance obtained from the current <see cref="DateTime"/> instance.</returns>
42+
public TimeOnly ToTimeOnly() => TimeOnly.FromDateTime(receiver);
43+
}
3944
}

OnixLabs.Core/Extensions.Random.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ namespace OnixLabs.Core;
2525
public static class RandomExtensions
2626
{
2727
/// <summary>
28-
/// Obtains a random element from the specified <see cref="IReadOnlyList{T}"/> items.
28+
/// Provides extension methods for <see cref="Random"/> instances.
2929
/// </summary>
3030
/// <param name="random">The current <see cref="Random"/> instance.</param>
31-
/// <param name="items">The <see cref="IReadOnlyList{T}"/> items from which to obtain a random element.</param>
32-
/// <typeparam name="T">The underlying type of the <see cref="IReadOnlyList{T}"/> collection.</typeparam>
33-
/// <returns>Returns a random element from the specified <see cref="IReadOnlyList{T}"/> items.</returns>
34-
public static T Next<T>(this Random random, IReadOnlyList<T> items) => items[random.Next(0, items.Count)];
31+
extension(Random random)
32+
{
33+
/// <summary>
34+
/// Obtains a random element from the specified <see cref="IReadOnlyList{T}"/> items.
35+
/// </summary>
36+
/// <param name="items">The <see cref="IReadOnlyList{T}"/> items from which to obtain a random element.</param>
37+
/// <typeparam name="T">The underlying type of the <see cref="IReadOnlyList{T}"/> collection.</typeparam>
38+
/// <returns>Returns a random element from the specified <see cref="IReadOnlyList{T}"/> items.</returns>
39+
public T Next<T>(IReadOnlyList<T> items) => items[random.Next(0, items.Count)];
40+
}
3541
}

OnixLabs.Core/Extensions.ReadOnlySequence.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,26 @@ namespace OnixLabs.Core;
2424
public static class ReadOnlySequenceExtensions
2525
{
2626
/// <summary>
27-
/// Copies the current <see cref="ReadOnlySequence{T}"/> to the specified <see cref="T:[]"/> by reference.
27+
/// Provides extension methods for <see cref="ReadOnlySequence{T}"/> instances.
2828
/// </summary>
29-
/// <param name="sequence">The current <see cref="ReadOnlySequence{T}"/> to copy.</param>
30-
/// <param name="array">The <see cref="T:[]"/> to copy to.</param>
31-
/// <typeparam name="T">The underlying type of the specified <see cref="ReadOnlySequence{T}"/> and <see cref="T:[]"/>.</typeparam>
32-
public static void CopyTo<T>(this ReadOnlySequence<T> sequence, out T[] array)
29+
/// <param name="sequence">The current <see cref="ReadOnlySequence{T}"/> instance.</param>
30+
/// <typeparam name="T">The underlying type of the current <see cref="ReadOnlySequence{T}"/> instance.</typeparam>
31+
extension<T>(ReadOnlySequence<T> sequence)
3332
{
34-
if (sequence.IsSingleSegment)
33+
/// <summary>
34+
/// Copies the current <see cref="ReadOnlySequence{T}"/> to the specified <typeparamref name="T"/> array.
35+
/// </summary>
36+
/// <param name="array">The <typeparamref name="T"/> array to copy in to.</param>
37+
public void CopyTo(out T[] array)
3538
{
36-
array = sequence.First.Span.ToArray();
37-
}
38-
else
39-
{
40-
// ReSharper disable once HeapView.ObjectAllocation.Evident
41-
array = new T[sequence.Length];
42-
sequence.CopyTo(array);
39+
if (sequence.IsSingleSegment)
40+
array = sequence.First.Span.ToArray();
41+
else
42+
{
43+
// ReSharper disable once HeapView.ObjectAllocation.Evident
44+
array = new T[sequence.Length];
45+
sequence.CopyTo(array);
46+
}
4347
}
4448
}
4549
}

0 commit comments

Comments
 (0)