1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:18:47 +00:00

🔨 Use block bodies instead of expression bodies

This commit is contained in:
Oliver Booth 2019-11-19 15:15:32 +00:00
parent a67fd274fa
commit 75a37d44d4
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
17 changed files with 524 additions and 262 deletions

View File

@ -21,8 +21,10 @@
/// </summary> /// </summary>
/// <param name="image">The image to convert.</param> /// <param name="image">The image to convert.</param>
/// <returns>Returns an <see cref="Image"/>.</returns> /// <returns>Returns an <see cref="Image"/>.</returns>
public static async Task<Image> ToSquareAsync(this Image image) => public static async Task<Image> ToSquareAsync(this Image image)
await Task.Run(image.ToSquare); {
return await Task.Run(image.ToSquare);
}
/// <summary> /// <summary>
/// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with /// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with
@ -31,8 +33,10 @@
/// <param name="image">The image to convert.</param> /// <param name="image">The image to convert.</param>
/// <param name="size">The new size of the image, i.e. the value of both the width and height.</param> /// <param name="size">The new size of the image, i.e. the value of both the width and height.</param>
/// <returns>Returns an <see cref="Image"/>.</returns> /// <returns>Returns an <see cref="Image"/>.</returns>
public static async Task<Image> ToSquareAsync(this Image image, int size) => public static async Task<Image> ToSquareAsync(this Image image, int size)
await Task.Run(() => image.ToSquare(size)); {
return await Task.Run(() => image.ToSquare(size));
}
/// <summary> /// <summary>
/// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with a /// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with a
@ -42,8 +46,10 @@
/// <param name="size">The new size of the image, i.e. the value of both the width and height.</param> /// <param name="size">The new size of the image, i.e. the value of both the width and height.</param>
/// <param name="background">The background color to fill.</param> /// <param name="background">The background color to fill.</param>
/// <returns>Returns an <see cref="Image"/>.</returns> /// <returns>Returns an <see cref="Image"/>.</returns>
public static async Task<Image> ToSquareAsync(this Image image, int size, Color background) => public static async Task<Image> ToSquareAsync(this Image image, int size, Color background)
await Task.Run(() => image.ToSquare(size, background)); {
return await Task.Run(() => image.ToSquare(size, background));
}
/// <summary> /// <summary>
/// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with a /// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with a
@ -52,16 +58,20 @@
/// <param name="image">The image to convert.</param> /// <param name="image">The image to convert.</param>
/// <param name="background">The background color to fill.</param> /// <param name="background">The background color to fill.</param>
/// <returns>Returns an <see cref="Image"/>.</returns> /// <returns>Returns an <see cref="Image"/>.</returns>
public static async Task<Image> ToSquareAsync(this Image image, Color background) => public static async Task<Image> ToSquareAsync(this Image image, Color background)
await Task.Run(() => image.ToSquare(background)); {
return await Task.Run(() => image.ToSquare(background));
}
/// <summary> /// <summary>
/// Converts the image so that its aspect ratio is 1:1, surrounding any new area with transparency. /// Converts the image so that its aspect ratio is 1:1, surrounding any new area with transparency.
/// </summary> /// </summary>
/// <param name="image">The image to convert.</param> /// <param name="image">The image to convert.</param>
/// <returns>Returns an <see cref="Image"/>.</returns> /// <returns>Returns an <see cref="Image"/>.</returns>
public static Image ToSquare(this Image image) => public static Image ToSquare(this Image image)
image.ToSquare(Color.Transparent); {
return image.ToSquare(Color.Transparent);
}
/// <summary> /// <summary>
/// Converts the image so that its aspect ratio is 1:1, surrounding any new area with transparency. /// Converts the image so that its aspect ratio is 1:1, surrounding any new area with transparency.
@ -69,8 +79,10 @@
/// <param name="image">The image to convert.</param> /// <param name="image">The image to convert.</param>
/// <param name="size">The new size of the image, i.e. the value of both the width and height.</param> /// <param name="size">The new size of the image, i.e. the value of both the width and height.</param>
/// <returns>Returns an <see cref="Image"/>.</returns> /// <returns>Returns an <see cref="Image"/>.</returns>
public static Image ToSquare(this Image image, int size) => public static Image ToSquare(this Image image, int size)
image.ToSquare(size, Color.Transparent); {
return image.ToSquare(size, Color.Transparent);
}
/// <summary> /// <summary>
/// Converts the image so that its aspect ratio is 1:1, surrounding any new area with a specified background /// Converts the image so that its aspect ratio is 1:1, surrounding any new area with a specified background
@ -117,8 +129,10 @@
/// <param name="width">The new width.</param> /// <param name="width">The new width.</param>
/// <param name="height">The new height.</param> /// <param name="height">The new height.</param>
/// <returns>Returns a new <see cref="Image"/>.</returns> /// <returns>Returns a new <see cref="Image"/>.</returns>
public static async Task<Bitmap> ScaleAsync(this Image image, int width, int height) => public static async Task<Bitmap> ScaleAsync(this Image image, int width, int height)
await Task.Run(() => image.Scale(width, height)); {
return await Task.Run(() => image.Scale(width, height));
}
/// <summary> /// <summary>
/// Asynchronously scales the image. /// Asynchronously scales the image.
@ -126,8 +140,10 @@
/// <param name="image">The image to scale.</param> /// <param name="image">The image to scale.</param>
/// <param name="factor">The scale factor.</param> /// <param name="factor">The scale factor.</param>
/// <returns>Returns a new <see cref="Image"/>.</returns> /// <returns>Returns a new <see cref="Image"/>.</returns>
public static async Task<Bitmap> ScaleAsync(this Image image, float factor) => public static async Task<Bitmap> ScaleAsync(this Image image, float factor)
await Task.Run(() => image.Scale(factor)); {
return await Task.Run(() => image.Scale(factor));
}
/// <summary> /// <summary>
/// Scales the image. /// Scales the image.
@ -135,8 +151,10 @@
/// <param name="image">The image to scale.</param> /// <param name="image">The image to scale.</param>
/// <param name="factor">The scale factor.</param> /// <param name="factor">The scale factor.</param>
/// <returns>Returns a new <see cref="Image"/>.</returns> /// <returns>Returns a new <see cref="Image"/>.</returns>
public static Bitmap Scale(this Image image, float factor) => public static Bitmap Scale(this Image image, float factor)
image.Scale((int)(image.Width * factor), (int)(image.Height * factor)); {
return image.Scale((int) (image.Width * factor), (int) (image.Height * factor));
}
/// <summary> /// <summary>
/// Scales the image. /// Scales the image.

View File

@ -32,31 +32,39 @@
/// <param name="v">The vector to round.</param> /// <param name="v">The vector to round.</param>
/// <param name="nearest">The nearest value.</param> /// <param name="nearest">The nearest value.</param>
/// <returns>Returns a <see cref="Vector3"/> containing the rounded values.</returns> /// <returns>Returns a <see cref="Vector3"/> containing the rounded values.</returns>
public static Vector3 Round(this Vector3 v, int nearest = 1) => public static Vector3 Round(this Vector3 v, int nearest = 1)
new Vector3(v.x.Round(nearest), v.y.Round(nearest), v.z.Round(nearest)); {
return new Vector3(v.x.Round(nearest), v.y.Round(nearest), v.z.Round(nearest));
}
/// <summary> /// <summary>
/// Inverts the X component of a vector. /// Inverts the X component of a vector.
/// </summary> /// </summary>
/// <param name="v">The vector to evaluate.</param> /// <param name="v">The vector to evaluate.</param>
/// <returns>Returns a <see cref="Vector3"/> whose values are (-X, Y, Z).</returns> /// <returns>Returns a <see cref="Vector3"/> whose values are (-X, Y, Z).</returns>
public static Vector3 InvertX(this Vector3 v) => public static Vector3 InvertX(this Vector3 v)
new Vector3(-v.x, v.y, v.z); {
return new Vector3(-v.x, v.y, v.z);
}
/// <summary> /// <summary>
/// Inverts the Y component of a vector. /// Inverts the Y component of a vector.
/// </summary> /// </summary>
/// <param name="v">The vector to evaluate.</param> /// <param name="v">The vector to evaluate.</param>
/// <returns>Returns a <see cref="Vector3"/> whose values are (X, -Y, Z).</returns> /// <returns>Returns a <see cref="Vector3"/> whose values are (X, -Y, Z).</returns>
public static Vector3 InvertY(this Vector3 v) => public static Vector3 InvertY(this Vector3 v)
new Vector3(v.x, -v.y, v.z); {
return new Vector3(v.x, -v.y, v.z);
}
/// <summary> /// <summary>
/// Inverts the Z component of a vector. /// Inverts the Z component of a vector.
/// </summary> /// </summary>
/// <param name="v">The vector to evaluate.</param> /// <param name="v">The vector to evaluate.</param>
/// <returns>Returns a <see cref="Vector3"/> whose values are (X, Y, -Z).</returns> /// <returns>Returns a <see cref="Vector3"/> whose values are (X, Y, -Z).</returns>
public static Vector3 InvertZ(this Vector3 v) => public static Vector3 InvertZ(this Vector3 v)
new Vector3(v.x, v.y, -v.z); {
return new Vector3(v.x, v.y, -v.z);
}
} }
} }

View File

@ -16,7 +16,9 @@
/// </summary> /// </summary>
/// <param name="value">The boolean.</param> /// <param name="value">The boolean.</param>
/// <returns>Returns 1 if <paramref name="value"/> is <see langword="true"/>, 0 otherwise.</returns> /// <returns>Returns 1 if <paramref name="value"/> is <see langword="true"/>, 0 otherwise.</returns>
public static int ToInt32(this bool value) => public static int ToInt32(this bool value)
value ? 1 : 0; {
return value ? 1 : 0;
}
} }
} }

View File

@ -19,56 +19,70 @@
/// </summary> /// </summary>
/// <param name="bytes">The bytes to get.</param> /// <param name="bytes">The bytes to get.</param>
/// <returns>Returns a <see cref="String"/>.</returns> /// <returns>Returns a <see cref="String"/>.</returns>
public static string AsString(this IEnumerable<byte> bytes) => public static string AsString(this IEnumerable<byte> bytes)
BitConverter.ToString(bytes.ToArray()); {
return BitConverter.ToString(bytes.ToArray());
}
/// <summary> /// <summary>
/// Converts the <see cref="Byte"/>[] to an <see cref="Int16"/>. /// Converts the <see cref="Byte"/>[] to an <see cref="Int16"/>.
/// </summary> /// </summary>
/// <param name="bytes">The bytes to convert.</param> /// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="Int16"/>.</returns> /// <returns>Returns an <see cref="Int16"/>.</returns>
public static short GetInt16(this IEnumerable<byte> bytes) => public static short GetInt16(this IEnumerable<byte> bytes)
BitConverter.ToInt16(bytes.ToArray(), 0); {
return BitConverter.ToInt16(bytes.ToArray(), 0);
}
/// <summary> /// <summary>
/// Converts the <see cref="Byte"/>[] to an <see cref="Int32"/>. /// Converts the <see cref="Byte"/>[] to an <see cref="Int32"/>.
/// </summary> /// </summary>
/// <param name="bytes">The bytes to convert.</param> /// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="Int32"/>.</returns> /// <returns>Returns an <see cref="Int32"/>.</returns>
public static int GetInt32(this IEnumerable<byte> bytes) => public static int GetInt32(this IEnumerable<byte> bytes)
BitConverter.ToInt32(bytes.ToArray(), 0); {
return BitConverter.ToInt32(bytes.ToArray(), 0);
}
/// <summary> /// <summary>
/// Converts the <see cref="Byte"/>[] to an <see cref="Int64"/>. /// Converts the <see cref="Byte"/>[] to an <see cref="Int64"/>.
/// </summary> /// </summary>
/// <param name="bytes">The bytes to convert.</param> /// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="Int64"/>.</returns> /// <returns>Returns an <see cref="Int64"/>.</returns>
public static long GetInt64(this IEnumerable<byte> bytes) => public static long GetInt64(this IEnumerable<byte> bytes)
BitConverter.ToInt64(bytes.ToArray(), 0); {
return BitConverter.ToInt64(bytes.ToArray(), 0);
}
/// <summary> /// <summary>
/// Converts the <see cref="Byte"/>[] to a <see cref="UInt16"/>. /// Converts the <see cref="Byte"/>[] to a <see cref="UInt16"/>.
/// </summary> /// </summary>
/// <param name="bytes">The bytes to convert.</param> /// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="UInt16"/>.</returns> /// <returns>Returns an <see cref="UInt16"/>.</returns>
public static ushort GetUInt16(this IEnumerable<byte> bytes) => public static ushort GetUInt16(this IEnumerable<byte> bytes)
BitConverter.ToUInt16(bytes.ToArray(), 0); {
return BitConverter.ToUInt16(bytes.ToArray(), 0);
}
/// <summary> /// <summary>
/// Converts the <see cref="Byte"/>[] to an <see cref="UInt32"/>. /// Converts the <see cref="Byte"/>[] to an <see cref="UInt32"/>.
/// </summary> /// </summary>
/// <param name="bytes">The bytes to convert.</param> /// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="UInt32"/>.</returns> /// <returns>Returns an <see cref="UInt32"/>.</returns>
public static uint GetUInt32(this IEnumerable<byte> bytes) => public static uint GetUInt32(this IEnumerable<byte> bytes)
BitConverter.ToUInt32(bytes.ToArray(), 0); {
return BitConverter.ToUInt32(bytes.ToArray(), 0);
}
/// <summary> /// <summary>
/// Converts the <see cref="Byte"/>[] to an <see cref="UInt64"/>. /// Converts the <see cref="Byte"/>[] to an <see cref="UInt64"/>.
/// </summary> /// </summary>
/// <param name="bytes">The bytes to convert.</param> /// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns an <see cref="UInt64"/>.</returns> /// <returns>Returns an <see cref="UInt64"/>.</returns>
public static ulong GetUInt64(this IEnumerable<byte> bytes) => public static ulong GetUInt64(this IEnumerable<byte> bytes)
BitConverter.ToUInt64(bytes.ToArray(), 0); {
return BitConverter.ToUInt64(bytes.ToArray(), 0);
}
/// <summary> /// <summary>
/// Gets a <see cref="String"/> representing the value the <see cref="Byte"/>[] with /// Gets a <see cref="String"/> representing the value the <see cref="Byte"/>[] with
@ -76,8 +90,10 @@
/// </summary> /// </summary>
/// <param name="bytes">The bytes to convert.</param> /// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns a <see cref="String"/>.</returns> /// <returns>Returns a <see cref="String"/>.</returns>
public static string GetString(this IEnumerable<byte> bytes) => public static string GetString(this IEnumerable<byte> bytes)
bytes.GetString(Encoding.UTF8); {
return bytes.GetString(Encoding.UTF8);
}
/// <summary> /// <summary>
/// Gets a <see cref="String"/> representing the value the <see cref="Byte"/>[] with the provided encoding. /// Gets a <see cref="String"/> representing the value the <see cref="Byte"/>[] with the provided encoding.

View File

@ -18,7 +18,9 @@
/// <param name="count">The repeat count.</param> /// <param name="count">The repeat count.</param>
/// <returns>Returns a <see cref="String"/> whose value is <paramref name="c"/> repeated /// <returns>Returns a <see cref="String"/> whose value is <paramref name="c"/> repeated
/// <paramref name="count"/> times.</returns> /// <paramref name="count"/> times.</returns>
public static string Repeat(this char c, int count) => public static string Repeat(this char c, int count)
new string(c, count); {
return new string(c, count);
}
} }
} }

View File

@ -20,7 +20,9 @@
/// <param name="upper">The exclusive upper bound.</param> /// <param name="upper">The exclusive upper bound.</param>
/// <returns>Returns <see langword="true"/> if the value is between the bounds, <see langword="false"/> /// <returns>Returns <see langword="true"/> if the value is between the bounds, <see langword="false"/>
/// otherwise.</returns> /// otherwise.</returns>
public static bool Between<T>(this T actual, T lower, T upper) where T : IComparable<T> => public static bool Between<T>(this T actual, T lower, T upper) where T : IComparable<T>
actual.CompareTo(lower) > 0 && actual.CompareTo(upper) < 0; {
return actual.CompareTo(lower) > 0 && actual.CompareTo(upper) < 0;
}
} }
} }

View File

@ -17,8 +17,10 @@
/// <typeparam name="T">The type to convert to.</typeparam> /// <typeparam name="T">The type to convert to.</typeparam>
/// <param name="obj">The object to convert.</param> /// <param name="obj">The object to convert.</param>
/// <returns>Returns the value converted to <see cref="T"/>.</returns> /// <returns>Returns the value converted to <see cref="T"/>.</returns>
public static T To<T>(this IConvertible obj) => public static T To<T>(this IConvertible obj)
(T)Convert.ChangeType(obj, typeof(T)); {
return (T) Convert.ChangeType(obj, typeof(T));
}
/// <summary> /// <summary>
/// Converts the object to another type, returning the default value on failure. /// Converts the object to another type, returning the default value on failure.

View File

@ -15,8 +15,10 @@
/// Calculates someone's age based on a date of birth. /// Calculates someone's age based on a date of birth.
/// </summary> /// </summary>
/// <param name="dateOfBirth">The date of birth.</param> /// <param name="dateOfBirth">The date of birth.</param>
public static int Age(this DateTime dateOfBirth) => public static int Age(this DateTime dateOfBirth)
(int)(((DateTime.Today - TimeSpan.FromDays(1) - dateOfBirth.Date).TotalDays + 1) / 365.2425); {
return (int) (((DateTime.Today - TimeSpan.FromDays(1) - dateOfBirth.Date).TotalDays + 1) / 365.2425);
}
/// <summary> /// <summary>
/// Gets a DateTime representing the first specified day in the current month /// Gets a DateTime representing the first specified day in the current month
@ -40,8 +42,10 @@
/// Gets a <see cref="DateTime"/> representing the first day in the current month. /// Gets a <see cref="DateTime"/> representing the first day in the current month.
/// </summary> /// </summary>
/// <param name="current">The current date.</param> /// <param name="current">The current date.</param>
public static DateTime FirstDayOfMonth(this DateTime current) => public static DateTime FirstDayOfMonth(this DateTime current)
current.AddDays(1 - current.Day); {
return current.AddDays(1 - current.Day);
}
/// <summary> /// <summary>
/// Gets a <see cref="DateTime"/> representing the last day in the current month. /// Gets a <see cref="DateTime"/> representing the last day in the current month.
@ -72,8 +76,10 @@
/// Gets a <see cref="DateTime"/> representing midnight on the current date. /// Gets a <see cref="DateTime"/> representing midnight on the current date.
/// </summary> /// </summary>
/// <param name="current">The current date.</param> /// <param name="current">The current date.</param>
public static DateTime Midnight(this DateTime current) => public static DateTime Midnight(this DateTime current)
new DateTime(current.Year, current.Month, current.Day); {
return new DateTime(current.Year, current.Month, current.Day);
}
/// <summary> /// <summary>
/// Gets a <see cref="DateTime"/> representing the first date following the current date which falls on the /// Gets a <see cref="DateTime"/> representing the first date following the current date which falls on the
@ -98,8 +104,10 @@
/// Gets a <see cref="DateTime"/> representing noon on the current date. /// Gets a <see cref="DateTime"/> representing noon on the current date.
/// </summary> /// </summary>
/// <param name="current">The current date.</param> /// <param name="current">The current date.</param>
public static DateTime Noon(this DateTime current) => public static DateTime Noon(this DateTime current)
new DateTime(current.Year, current.Month, current.Day, 12, 0, 0); {
return new DateTime(current.Year, current.Month, current.Day, 12, 0, 0);
}
/// <summary> /// <summary>
/// Sets the time of the current date with minute precision. /// Sets the time of the current date with minute precision.
@ -107,8 +115,10 @@
/// <param name="current">The current date.</param> /// <param name="current">The current date.</param>
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
public static DateTime SetTime(this DateTime current, int hour, int minute) => public static DateTime SetTime(this DateTime current, int hour, int minute)
current.SetTime(hour, minute, 0, 0); {
return current.SetTime(hour, minute, 0, 0);
}
/// <summary> /// <summary>
/// Sets the time of the current date with second precision. /// Sets the time of the current date with second precision.
@ -118,8 +128,10 @@
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
/// <returns></returns> /// <returns></returns>
public static DateTime SetTime(this DateTime current, int hour, int minute, int second) => public static DateTime SetTime(this DateTime current, int hour, int minute, int second)
current.SetTime(hour, minute, second, 0); {
return current.SetTime(hour, minute, second, 0);
}
/// <summary> /// <summary>
/// Sets the time of the current date with millisecond precision. /// Sets the time of the current date with millisecond precision.
@ -129,8 +141,10 @@
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
/// <param name="millisecond">The millisecond.</param> /// <param name="millisecond">The millisecond.</param>
public static DateTime SetTime(this DateTime current, int hour, int minute, int second, int millisecond) => public static DateTime SetTime(this DateTime current, int hour, int minute, int second, int millisecond)
new DateTime(current.Year, current.Month, current.Day, hour, minute, second, millisecond); {
return new DateTime(current.Year, current.Month, current.Day, hour, minute, second, millisecond);
}
/// <summary> /// <summary>
/// Converts the <see cref="DateTime"/> to a Unix timestamp. /// Converts the <see cref="DateTime"/> to a Unix timestamp.
@ -151,7 +165,9 @@
/// <param name="date">The <see cref="DateTime"/> to copy.</param> /// <param name="date">The <see cref="DateTime"/> to copy.</param>
/// <param name="year">The year to set.</param> /// <param name="year">The year to set.</param>
/// <returns>Returns a <see cref="DateTime"/>.</returns> /// <returns>Returns a <see cref="DateTime"/>.</returns>
public static DateTime WithYear(this DateTime date, int year) => public static DateTime WithYear(this DateTime date, int year)
new DateTime(year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Millisecond); {
return new DateTime(year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Millisecond);
}
} }
} }

View File

@ -20,32 +20,40 @@
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it, /// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it, /// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns> /// or <paramref name="value"/> itself otherwise.</returns>
public static double Clamp(this double value, double min, double max) => public static double Clamp(this double value, double min, double max)
Math.Min(Math.Max(value, min), max); {
return Math.Min(Math.Max(value, min), max);
}
/// <summary> /// <summary>
/// Converts an angle from degrees to radians. /// Converts an angle from degrees to radians.
/// </summary> /// </summary>
/// <param name="angle">The angle in degrees.</param> /// <param name="angle">The angle in degrees.</param>
/// <returns>Returns <paramref name="angle"/> in radians.</returns> /// <returns>Returns <paramref name="angle"/> in radians.</returns>
public static double DegreesToRadians(this double angle) => public static double DegreesToRadians(this double angle)
Math.PI * angle / 180.0; {
return Math.PI * angle / 180.0;
}
/// <summary> /// <summary>
/// Converts the <see cref="Double"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="Double"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>
/// <param name="number">The number to convert.</param> /// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this double number) => public static byte[] GetBytes(this double number)
BitConverter.GetBytes(number); {
return BitConverter.GetBytes(number);
}
/// <summary> /// <summary>
/// Converts an angle from radians to degrees. /// Converts an angle from radians to degrees.
/// </summary> /// </summary>
/// <param name="angle">The angle in radians.</param> /// <param name="angle">The angle in radians.</param>
/// <returns>Returns <paramref name="angle"/> in degrees.</returns> /// <returns>Returns <paramref name="angle"/> in degrees.</returns>
public static double RadiansToDegrees(this double angle) => public static double RadiansToDegrees(this double angle)
angle * (180.0 / Math.PI); {
return angle * (180.0 / Math.PI);
}
/// <summary> /// <summary>
/// Rounds to the nearest value. /// Rounds to the nearest value.
@ -53,7 +61,9 @@
/// <param name="v">The value to round.</param> /// <param name="v">The value to round.</param>
/// <param name="nearest">The nearest value.</param> /// <param name="nearest">The nearest value.</param>
/// <returns>Returns the rounded value.</returns> /// <returns>Returns the rounded value.</returns>
public static double Round(this double v, int nearest = 1) => public static double Round(this double v, int nearest = 1)
Math.Round(v / nearest) * nearest; {
return Math.Round(v / nearest) * nearest;
}
} }
} }

View File

@ -18,25 +18,29 @@
/// <param name="endPoint">The endpoint whose hostname to get.</param> /// <param name="endPoint">The endpoint whose hostname to get.</param>
/// <returns>Returns a <see cref="String"/> representing the hostname, which may be an IP or a DNS, or empty /// <returns>Returns a <see cref="String"/> representing the hostname, which may be an IP or a DNS, or empty
/// string on failure.</returns> /// string on failure.</returns>
public static string GetHost(this EndPoint endPoint) => public static string GetHost(this EndPoint endPoint)
endPoint switch {
return endPoint switch
{ {
IPEndPoint ip => ip.Address.ToString(), IPEndPoint ip => ip.Address.ToString(),
DnsEndPoint dns => dns.Host, DnsEndPoint dns => dns.Host,
_ => String.Empty _ => String.Empty
}; };
}
/// <summary> /// <summary>
/// Gets the endpoint port. /// Gets the endpoint port.
/// </summary> /// </summary>
/// <param name="endPoint">The endpoint whose port to get.</param> /// <param name="endPoint">The endpoint whose port to get.</param>
/// <returns>Returns an <see cref="Int32"/> representing the port, or 0 on failure.</returns> /// <returns>Returns an <see cref="Int32"/> representing the port, or 0 on failure.</returns>
public static int GetPort(this EndPoint endPoint) => public static int GetPort(this EndPoint endPoint)
endPoint switch {
return endPoint switch
{ {
IPEndPoint ip => ip.Port, IPEndPoint ip => ip.Port,
DnsEndPoint dns => dns.Port, DnsEndPoint dns => dns.Port,
_ => 0 _ => 0
}; };
}
} }
} }

View File

@ -15,41 +15,65 @@
// TODO change // TODO change
public static TimeSpan Days(this ushort number) => public static TimeSpan Days(this ushort number)
TimeSpan.FromDays(number); {
return TimeSpan.FromDays(number);
}
public static TimeSpan Hours(this ushort number) => public static TimeSpan Hours(this ushort number)
TimeSpan.FromHours(number); {
return TimeSpan.FromHours(number);
}
public static TimeSpan Milliseconds(this ushort number) => public static TimeSpan Milliseconds(this ushort number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Minutes(this ushort number) => public static TimeSpan Minutes(this ushort number)
TimeSpan.FromMinutes(number); {
return TimeSpan.FromMinutes(number);
}
public static TimeSpan Seconds(this ushort number) => public static TimeSpan Seconds(this ushort number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Ticks(this ushort number) => public static TimeSpan Ticks(this ushort number)
TimeSpan.FromTicks(number); {
return TimeSpan.FromTicks(number);
}
public static TimeSpan Days(this short number) => public static TimeSpan Days(this short number)
TimeSpan.FromDays(number); {
return TimeSpan.FromDays(number);
}
public static TimeSpan Hours(this short number) => public static TimeSpan Hours(this short number)
TimeSpan.FromHours(number); {
return TimeSpan.FromHours(number);
}
public static TimeSpan Milliseconds(this short number) => public static TimeSpan Milliseconds(this short number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Minutes(this short number) => public static TimeSpan Minutes(this short number)
TimeSpan.FromMinutes(number); {
return TimeSpan.FromMinutes(number);
}
public static TimeSpan Seconds(this short number) => public static TimeSpan Seconds(this short number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Ticks(this short number) => public static TimeSpan Ticks(this short number)
TimeSpan.FromTicks(number); {
return TimeSpan.FromTicks(number);
}
#endregion #endregion
@ -62,8 +86,10 @@
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it, /// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it, /// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns> /// or <paramref name="value"/> itself otherwise.</returns>
public static short Clamp(this short value, short min, short max) => public static short Clamp(this short value, short min, short max)
Math.Min(Math.Max(value, min), max); {
return Math.Min(Math.Max(value, min), max);
}
/// <summary> /// <summary>
/// Clamps a value between a minimum and a maximum value. /// Clamps a value between a minimum and a maximum value.
@ -74,8 +100,10 @@
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it, /// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it, /// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns> /// or <paramref name="value"/> itself otherwise.</returns>
public static ushort Clamp(this ushort value, ushort min, ushort max) => public static ushort Clamp(this ushort value, ushort min, ushort max)
Math.Min(Math.Max(value, min), max); {
return Math.Min(Math.Max(value, min), max);
}
/// <summary> /// <summary>
/// Converts the <see cref="Int16"/> to a <see cref="DateTime"/> treating it as a Unix timestamp. /// Converts the <see cref="Int16"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
@ -85,24 +113,30 @@
/// to <see langword="false"/>..</param> /// to <see langword="false"/>..</param>
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix /// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
/// epoch.</returns> /// epoch.</returns>
public static DateTime FromUnixTimestamp(this short timestamp, bool isMillis = false) => public static DateTime FromUnixTimestamp(this short timestamp, bool isMillis = false)
((long)timestamp).FromUnixTimestamp(isMillis); {
return ((long) timestamp).FromUnixTimestamp(isMillis);
}
/// <summary> /// <summary>
/// Converts the <see cref="UInt16"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="UInt16"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>
/// <param name="number">The number to convert.</param> /// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this ushort number) => public static byte[] GetBytes(this ushort number)
BitConverter.GetBytes(number); {
return BitConverter.GetBytes(number);
}
/// <summary> /// <summary>
/// Converts the <see cref="Int16"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="Int16"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>
/// <param name="number">The number to convert.</param> /// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this short number) => public static byte[] GetBytes(this short number)
BitConverter.GetBytes(number); {
return BitConverter.GetBytes(number);
}
/// <summary> /// <summary>
/// Determines if the <see cref="Int16"/> is a prime number. /// Determines if the <see cref="Int16"/> is a prime number.
@ -110,8 +144,10 @@
/// <param name="number">The number.</param> /// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/> /// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
/// otherwise.</returns> /// otherwise.</returns>
public static bool IsPrime(this short number) => public static bool IsPrime(this short number)
((long)number).IsPrime(); {
return ((long) number).IsPrime();
}
/// <summary> /// <summary>
/// Gets an boolean value that represents this integer. /// Gets an boolean value that represents this integer.
@ -119,8 +155,10 @@
/// <param name="value">The integer.</param> /// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0, /// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns> /// <see langword="true"/> otherwise.</returns>
public static bool ToBoolean(this short value) => public static bool ToBoolean(this short value)
((long)value).ToBoolean(); {
return ((long) value).ToBoolean();
}
/// <summary> /// <summary>
/// Gets an boolean value that represents this integer. /// Gets an boolean value that represents this integer.
@ -128,7 +166,9 @@
/// <param name="value">The integer.</param> /// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0, /// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns> /// <see langword="true"/> otherwise.</returns>
public static bool ToBoolean(this ushort value) => public static bool ToBoolean(this ushort value)
((ulong)value).ToBoolean(); {
return ((ulong) value).ToBoolean();
}
} }
} }

View File

@ -21,8 +21,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime January(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime January(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 1, day, hour, minute, second); {
return new DateTime(year, 1, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is February. /// Returns a <see cref="DateTime"/> where the month is February.
@ -32,8 +34,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime February(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime February(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 2, day, hour, minute, second); {
return new DateTime(year, 2, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is March. /// Returns a <see cref="DateTime"/> where the month is March.
@ -43,8 +47,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime March(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime March(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 3, day, hour, minute, second); {
return new DateTime(year, 3, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is April. /// Returns a <see cref="DateTime"/> where the month is April.
@ -54,8 +60,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime April(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime April(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 4, day, hour, minute, second); {
return new DateTime(year, 4, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is May. /// Returns a <see cref="DateTime"/> where the month is May.
@ -65,8 +73,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime May(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime May(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 5, day, hour, minute, second); {
return new DateTime(year, 5, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is June. /// Returns a <see cref="DateTime"/> where the month is June.
@ -76,8 +86,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime June(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime June(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 6, day, hour, minute, second); {
return new DateTime(year, 6, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is July. /// Returns a <see cref="DateTime"/> where the month is July.
@ -87,8 +99,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime July(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime July(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 7, day, hour, minute, second); {
return new DateTime(year, 7, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is August. /// Returns a <see cref="DateTime"/> where the month is August.
@ -98,8 +112,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime August(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime August(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 8, day, hour, minute, second); {
return new DateTime(year, 8, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is September. /// Returns a <see cref="DateTime"/> where the month is September.
@ -109,8 +125,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime September(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime September(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 9, day, hour, minute, second); {
return new DateTime(year, 9, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is October. /// Returns a <see cref="DateTime"/> where the month is October.
@ -120,8 +138,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime October(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime October(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 10, day, hour, minute, second); {
return new DateTime(year, 10, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is November. /// Returns a <see cref="DateTime"/> where the month is November.
@ -131,8 +151,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime November(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime November(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 11, day, hour, minute, second); {
return new DateTime(year, 11, day, hour, minute, second);
}
/// <summary> /// <summary>
/// Returns a <see cref="DateTime"/> where the month is December. /// Returns a <see cref="DateTime"/> where the month is December.
@ -142,8 +164,10 @@
/// <param name="hour">The hour.</param> /// <param name="hour">The hour.</param>
/// <param name="minute">The minute.</param> /// <param name="minute">The minute.</param>
/// <param name="second">The second.</param> /// <param name="second">The second.</param>
public static DateTime December(this int day, int year, int hour = 0, int minute = 0, int second = 0) => public static DateTime December(this int day, int year, int hour = 0, int minute = 0, int second = 0)
new DateTime(year, 12, day, hour, minute, second); {
return new DateTime(year, 12, day, hour, minute, second);
}
#endregion #endregion
@ -151,41 +175,65 @@
// TODO change // TODO change
public static TimeSpan Days(this uint number) => public static TimeSpan Days(this uint number)
TimeSpan.FromDays(number); {
return TimeSpan.FromDays(number);
}
public static TimeSpan Hours(this uint number) => public static TimeSpan Hours(this uint number)
TimeSpan.FromHours(number); {
return TimeSpan.FromHours(number);
}
public static TimeSpan Milliseconds(this uint number) => public static TimeSpan Milliseconds(this uint number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Minutes(this uint number) => public static TimeSpan Minutes(this uint number)
TimeSpan.FromMinutes(number); {
return TimeSpan.FromMinutes(number);
}
public static TimeSpan Seconds(this uint number) => public static TimeSpan Seconds(this uint number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Ticks(this uint number) => public static TimeSpan Ticks(this uint number)
TimeSpan.FromTicks(number); {
return TimeSpan.FromTicks(number);
}
public static TimeSpan Days(this int number) => public static TimeSpan Days(this int number)
TimeSpan.FromDays(number); {
return TimeSpan.FromDays(number);
}
public static TimeSpan Hours(this int number) => public static TimeSpan Hours(this int number)
TimeSpan.FromHours(number); {
return TimeSpan.FromHours(number);
}
public static TimeSpan Milliseconds(this int number) => public static TimeSpan Milliseconds(this int number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Minutes(this int number) => public static TimeSpan Minutes(this int number)
TimeSpan.FromMinutes(number); {
return TimeSpan.FromMinutes(number);
}
public static TimeSpan Seconds(this int number) => public static TimeSpan Seconds(this int number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Ticks(this int number) => public static TimeSpan Ticks(this int number)
TimeSpan.FromTicks(number); {
return TimeSpan.FromTicks(number);
}
#endregion #endregion
@ -198,8 +246,10 @@
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it, /// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it, /// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns> /// or <paramref name="value"/> itself otherwise.</returns>
public static int Clamp(this int value, int min, int max) => public static int Clamp(this int value, int min, int max)
Math.Min(Math.Max(value, min), max); {
return Math.Min(Math.Max(value, min), max);
}
/// <summary> /// <summary>
/// Clamps a value between a minimum and a maximum value. /// Clamps a value between a minimum and a maximum value.
@ -210,8 +260,10 @@
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it, /// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it, /// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns> /// or <paramref name="value"/> itself otherwise.</returns>
public static uint Clamp(this uint value, uint min, uint max) => public static uint Clamp(this uint value, uint min, uint max)
Math.Min(Math.Max(value, min), max); {
return Math.Min(Math.Max(value, min), max);
}
/// <summary> /// <summary>
/// Converts the <see cref="Int32"/> to a <see cref="DateTime"/> treating it as a Unix timestamp. /// Converts the <see cref="Int32"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
@ -221,24 +273,30 @@
/// to <see langword="false"/>..</param> /// to <see langword="false"/>..</param>
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix /// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
/// epoch.</returns> /// epoch.</returns>
public static DateTime FromUnixTimestamp(this int timestamp, bool isMillis = false) => public static DateTime FromUnixTimestamp(this int timestamp, bool isMillis = false)
((long)timestamp).FromUnixTimestamp(isMillis); {
return ((long) timestamp).FromUnixTimestamp(isMillis);
}
/// <summary> /// <summary>
/// Converts the <see cref="UInt32"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="UInt32"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>
/// <param name="number">The number to convert.</param> /// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this uint number) => public static byte[] GetBytes(this uint number)
BitConverter.GetBytes(number); {
return BitConverter.GetBytes(number);
}
/// <summary> /// <summary>
/// Converts the <see cref="Int32"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="Int32"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>
/// <param name="number">The number to convert.</param> /// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this int number) => public static byte[] GetBytes(this int number)
BitConverter.GetBytes(number); {
return BitConverter.GetBytes(number);
}
/// <summary> /// <summary>
/// Determines if the <see cref="Int32"/> is a prime number. /// Determines if the <see cref="Int32"/> is a prime number.
@ -246,8 +304,10 @@
/// <param name="number">The number.</param> /// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/> /// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
/// otherwise.</returns> /// otherwise.</returns>
public static bool IsPrime(this int number) => public static bool IsPrime(this int number)
((long)number).IsPrime(); {
return ((long) number).IsPrime();
}
/// <summary> /// <summary>
/// Gets an boolean value that represents this integer. /// Gets an boolean value that represents this integer.
@ -255,8 +315,10 @@
/// <param name="value">The integer.</param> /// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0, /// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns> /// <see langword="true"/> otherwise.</returns>
public static bool ToBoolean(this int value) => public static bool ToBoolean(this int value)
((long)value).ToBoolean(); {
return ((long) value).ToBoolean();
}
/// <summary> /// <summary>
/// Gets an boolean value that represents this integer. /// Gets an boolean value that represents this integer.
@ -264,7 +326,9 @@
/// <param name="value">The integer.</param> /// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0, /// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns> /// <see langword="true"/> otherwise.</returns>
public static bool ToBoolean(this uint value) => public static bool ToBoolean(this uint value)
((ulong)value).ToBoolean(); {
return ((ulong) value).ToBoolean();
}
} }
} }

View File

@ -15,38 +15,60 @@
// TODO change // TODO change
public static TimeSpan Days(this ulong number) => public static TimeSpan Days(this ulong number)
TimeSpan.FromDays(number); {
return TimeSpan.FromDays(number);
}
public static TimeSpan Hours(this ulong number) => public static TimeSpan Hours(this ulong number)
TimeSpan.FromHours(number); {
return TimeSpan.FromHours(number);
}
public static TimeSpan Milliseconds(this ulong number) => public static TimeSpan Milliseconds(this ulong number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Minutes(this ulong number) => public static TimeSpan Minutes(this ulong number)
TimeSpan.FromMinutes(number); {
return TimeSpan.FromMinutes(number);
}
public static TimeSpan Seconds(this ulong number) => public static TimeSpan Seconds(this ulong number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Days(this long number) => public static TimeSpan Days(this long number)
TimeSpan.FromDays(number); {
return TimeSpan.FromDays(number);
}
public static TimeSpan Hours(this long number) => public static TimeSpan Hours(this long number)
TimeSpan.FromHours(number); {
return TimeSpan.FromHours(number);
}
public static TimeSpan Milliseconds(this long number) => public static TimeSpan Milliseconds(this long number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Minutes(this long number) => public static TimeSpan Minutes(this long number)
TimeSpan.FromMinutes(number); {
return TimeSpan.FromMinutes(number);
}
public static TimeSpan Seconds(this long number) => public static TimeSpan Seconds(this long number)
TimeSpan.FromSeconds(number); {
return TimeSpan.FromSeconds(number);
}
public static TimeSpan Ticks(this long number) => public static TimeSpan Ticks(this long number)
TimeSpan.FromTicks(number); {
return TimeSpan.FromTicks(number);
}
#endregion #endregion
@ -59,8 +81,10 @@
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it, /// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it, /// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns> /// or <paramref name="value"/> itself otherwise.</returns>
public static long Clamp(this long value, long min, long max) => public static long Clamp(this long value, long min, long max)
Math.Min(Math.Max(value, min), max); {
return Math.Min(Math.Max(value, min), max);
}
/// <summary> /// <summary>
/// Clamps a value between a minimum and a maximum value. /// Clamps a value between a minimum and a maximum value.
@ -71,8 +95,10 @@
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it, /// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it, /// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns> /// or <paramref name="value"/> itself otherwise.</returns>
public static ulong Clamp(this ulong value, ulong min, ulong max) => public static ulong Clamp(this ulong value, ulong min, ulong max)
Math.Min(Math.Max(value, min), max); {
return Math.Min(Math.Max(value, min), max);
}
/// <summary> /// <summary>
/// Converts the <see cref="Int64"/> to a <see cref="DateTime"/> treating it as a Unix timestamp. /// Converts the <see cref="Int64"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
@ -96,16 +122,20 @@
/// </summary> /// </summary>
/// <param name="number">The number to convert.</param> /// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this ulong number) => public static byte[] GetBytes(this ulong number)
BitConverter.GetBytes(number); {
return BitConverter.GetBytes(number);
}
/// <summary> /// <summary>
/// Converts the <see cref="Int64"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="Int64"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>
/// <param name="number">The number to convert.</param> /// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this long number) => public static byte[] GetBytes(this long number)
BitConverter.GetBytes(number); {
return BitConverter.GetBytes(number);
}
/// <summary> /// <summary>
/// Determines if the <see cref="Int64"/> is a prime number. /// Determines if the <see cref="Int64"/> is a prime number.
@ -148,8 +178,10 @@
/// <param name="value">The integer.</param> /// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0, /// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns> /// <see langword="true"/> otherwise.</returns>
public static bool ToBoolean(this long value) => public static bool ToBoolean(this long value)
value != 0; {
return value != 0;
}
/// <summary> /// <summary>
/// Gets an boolean value that represents this integer. /// Gets an boolean value that represents this integer.
@ -157,7 +189,9 @@
/// <param name="value">The integer.</param> /// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0, /// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns> /// <see langword="true"/> otherwise.</returns>
public static bool ToBoolean(this ulong value) => public static bool ToBoolean(this ulong value)
value != 0; {
return value != 0;
}
} }
} }

View File

@ -19,8 +19,10 @@
/// <typeparam name="T">The collection type.</typeparam> /// <typeparam name="T">The collection type.</typeparam>
/// <param name="source">The collection to draw from.</param> /// <param name="source">The collection to draw from.</param>
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns> /// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
public static T OneOf<T>(this IEnumerable<T> source) => public static T OneOf<T>(this IEnumerable<T> source)
source.OneOf(new Random()); {
return source.OneOf(new Random());
}
/// <summary> /// <summary>
/// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance. /// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance.
@ -29,8 +31,10 @@
/// <param name="source">The collection to draw from.</param> /// <param name="source">The collection to draw from.</param>
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="Random"/> instance.</param>
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns> /// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
public static T OneOf<T>(this IEnumerable<T> source, Random random) => public static T OneOf<T>(this IEnumerable<T> source, Random random)
source.ToList().OneOf(random); {
return source.ToList().OneOf(random);
}
/// <summary> /// <summary>
/// Returns a random element from <paramref name="source"/> using a new <see cref="Random"/> instance. /// Returns a random element from <paramref name="source"/> using a new <see cref="Random"/> instance.
@ -38,8 +42,10 @@
/// <typeparam name="T">The collection type.</typeparam> /// <typeparam name="T">The collection type.</typeparam>
/// <param name="source">The collection to draw from.</param> /// <param name="source">The collection to draw from.</param>
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns> /// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
public static T OneOf<T>(this IList<T> source) => public static T OneOf<T>(this IList<T> source)
source.OneOf(new Random()); {
return source.OneOf(new Random());
}
/// <summary> /// <summary>
/// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance. /// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance.
@ -48,8 +54,10 @@
/// <param name="source">The collection to draw from.</param> /// <param name="source">The collection to draw from.</param>
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="Random"/> instance.</param>
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns> /// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
public static T OneOf<T>(this IList<T> source, Random random) => public static T OneOf<T>(this IList<T> source, Random random)
random.OneOf(source); {
return random.OneOf(source);
}
/// <summary> /// <summary>
/// Shuffles an enumerable. /// Shuffles an enumerable.
@ -57,8 +65,10 @@
/// <typeparam name="T">The collection type.</typeparam> /// <typeparam name="T">The collection type.</typeparam>
/// <param name="source">The collection to shuffle.</param> /// <param name="source">The collection to shuffle.</param>
/// <returns>Returns <paramref name="source"/> shuffled.</returns> /// <returns>Returns <paramref name="source"/> shuffled.</returns>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) => public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
source.Shuffle(new Random()); {
return source.Shuffle(new Random());
}
/// <summary> /// <summary>
/// Shuffles an enumerable. /// Shuffles an enumerable.
@ -67,8 +77,10 @@
/// <param name="source">The collection to shuffle.</param> /// <param name="source">The collection to shuffle.</param>
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="Random"/> instance.</param>
/// <returns>Returns <paramref name="source"/> shuffled.</returns> /// <returns>Returns <paramref name="source"/> shuffled.</returns>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random random) => public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random random)
source.OrderBy(_ => random.Next()); {
return source.OrderBy(_ => random.Next());
}
/// <summary> /// <summary>
/// Shuffles a list. /// Shuffles a list.
@ -76,8 +88,10 @@
/// <typeparam name="T">The collection type.</typeparam> /// <typeparam name="T">The collection type.</typeparam>
/// <param name="source">The collection to shuffle.</param> /// <param name="source">The collection to shuffle.</param>
/// <returns>Returns <paramref name="source"/> shuffled.</returns> /// <returns>Returns <paramref name="source"/> shuffled.</returns>
public static IEnumerable<T> Shuffle<T>(this IList<T> source) => public static IEnumerable<T> Shuffle<T>(this IList<T> source)
source.Shuffle(new Random()); {
return source.Shuffle(new Random());
}
/// <summary> /// <summary>
/// Shuffles a list. /// Shuffles a list.
@ -86,7 +100,9 @@
/// <param name="source">The collection to shuffle.</param> /// <param name="source">The collection to shuffle.</param>
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="Random"/> instance.</param>
/// <returns>Returns <paramref name="source"/> shuffled.</returns> /// <returns>Returns <paramref name="source"/> shuffled.</returns>
public static IEnumerable<T> Shuffle<T>(this IList<T> source, Random random) => public static IEnumerable<T> Shuffle<T>(this IList<T> source, Random random)
source.OrderBy(_ => random.Next()); {
return source.OrderBy(_ => random.Next());
}
} }
} }

View File

@ -18,8 +18,10 @@
/// generation. /// generation.
/// </summary> /// </summary>
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="Random"/> instance.</param>
public static bool CoinToss(this Random random) => public static bool CoinToss(this Random random)
random.Next(2) == 0; {
return random.Next(2) == 0;
}
/// <summary> /// <summary>
/// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance. /// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance.
@ -28,8 +30,10 @@
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="Random"/> instance.</param>
/// <param name="source">The collection to draw from.</param> /// <param name="source">The collection to draw from.</param>
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns> /// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
public static T OneOf<T>(this Random random, params T[] source) => public static T OneOf<T>(this Random random, params T[] source)
source.ToList().OneOf(random); {
return source.ToList().OneOf(random);
}
/// <summary> /// <summary>
/// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance. /// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance.
@ -38,7 +42,9 @@
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="Random"/> instance.</param>
/// <param name="source">The collection to draw from.</param> /// <param name="source">The collection to draw from.</param>
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns> /// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
public static T OneOf<T>(this Random random, IList<T> source) => public static T OneOf<T>(this Random random, IList<T> source)
source[random.Next(source.Count)]; {
return source[random.Next(source.Count)];
}
} }
} }

View File

@ -20,32 +20,40 @@
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it, /// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <paramref name="min"/> if <paramref name="value"/> is less than it, /// <paramref name="min"/> if <paramref name="value"/> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns> /// or <paramref name="value"/> itself otherwise.</returns>
public static float Clamp(this float value, float min, float max) => public static float Clamp(this float value, float min, float max)
Math.Min(Math.Max(value, min), max); {
return Math.Min(Math.Max(value, min), max);
}
/// <summary> /// <summary>
/// Converts an angle from degrees to radians. /// Converts an angle from degrees to radians.
/// </summary> /// </summary>
/// <param name="angle">The angle in degrees.</param> /// <param name="angle">The angle in degrees.</param>
/// <returns>Returns <paramref name="angle"/> in radians.</returns> /// <returns>Returns <paramref name="angle"/> in radians.</returns>
public static float DegreesToRadians(this float angle) => public static float DegreesToRadians(this float angle)
(float)((double)angle).DegreesToRadians(); {
return (float) ((double) angle).DegreesToRadians();
}
/// <summary> /// <summary>
/// Converts the <see cref="Single"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="Single"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>
/// <param name="number">The number to convert.</param> /// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this float number) => public static byte[] GetBytes(this float number)
BitConverter.GetBytes(number); {
return BitConverter.GetBytes(number);
}
/// <summary> /// <summary>
/// Converts an angle from radians to degrees. /// Converts an angle from radians to degrees.
/// </summary> /// </summary>
/// <param name="angle">The angle in radians.</param> /// <param name="angle">The angle in radians.</param>
/// <returns>Returns <paramref name="angle"/> in degrees.</returns> /// <returns>Returns <paramref name="angle"/> in degrees.</returns>
public static float RadiansToDegrees(this float angle) => public static float RadiansToDegrees(this float angle)
(float)((double)angle).RadiansToDegrees(); {
return (float) ((double) angle).RadiansToDegrees();
}
/// <summary> /// <summary>
/// Rounds to the nearest value. /// Rounds to the nearest value.
@ -53,7 +61,9 @@
/// <param name="v">The value to round.</param> /// <param name="v">The value to round.</param>
/// <param name="nearest">The nearest value.</param> /// <param name="nearest">The nearest value.</param>
/// <returns>Returns the rounded value.</returns> /// <returns>Returns the rounded value.</returns>
public static float Round(this float v, int nearest = 1) => public static float Round(this float v, int nearest = 1)
(float)((double)v).Round(nearest); {
return (float) ((double) v).Round(nearest);
}
} }
} }

View File

@ -19,16 +19,20 @@
/// </summary> /// </summary>
/// <param name="data">The base-64 string to decode.</param> /// <param name="data">The base-64 string to decode.</param>
/// <returns>Returns the string in plain text.</returns> /// <returns>Returns the string in plain text.</returns>
public static string Base64Decode(this string data) => public static string Base64Decode(this string data)
Convert.FromBase64String(data).GetString(); {
return Convert.FromBase64String(data).GetString();
}
/// <summary> /// <summary>
/// Encodes a base-64 encoded string. /// Encodes a base-64 encoded string.
/// </summary> /// </summary>
/// <param name="value">The plain text string to decode.</param> /// <param name="value">The plain text string to decode.</param>
/// <returns>Returns the string in plain text.</returns> /// <returns>Returns the string in plain text.</returns>
public static string Base64Encode(this string value) => public static string Base64Encode(this string value)
Convert.ToBase64String(value.GetBytes()); {
return Convert.ToBase64String(value.GetBytes());
}
/// <summary> /// <summary>
/// Parses a <see cref="String"/> into an <see cref="Enum"/>. /// Parses a <see cref="String"/> into an <see cref="Enum"/>.
@ -36,8 +40,10 @@
/// <typeparam name="T">The type of the Enum</typeparam> /// <typeparam name="T">The type of the Enum</typeparam>
/// <param name="value">String value to parse</param> /// <param name="value">String value to parse</param>
/// <returns>The Enum corresponding to the stringExtensions</returns> /// <returns>The Enum corresponding to the stringExtensions</returns>
public static T EnumParse<T>(this string value) => public static T EnumParse<T>(this string value)
value.EnumParse<T>(false); {
return value.EnumParse<T>(false);
}
/// <summary> /// <summary>
/// Parses a <see cref="String"/> into an <see cref="Enum"/>. /// Parses a <see cref="String"/> into an <see cref="Enum"/>.
@ -77,8 +83,10 @@
/// </summary> /// </summary>
/// <param name="str">The string to convert.</param> /// <param name="str">The string to convert.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this string str) => public static byte[] GetBytes(this string str)
str.GetBytes(Encoding.UTF8); {
return str.GetBytes(Encoding.UTF8);
}
/// <summary> /// <summary>
/// Gets a <see cref="Byte"/>[] representing the value the <see cref="String"/> with the provided encoding. /// Gets a <see cref="Byte"/>[] representing the value the <see cref="String"/> with the provided encoding.
@ -86,8 +94,10 @@
/// <param name="str">The string to convert.</param> /// <param name="str">The string to convert.</param>
/// <param name="encoding">The encoding to use.</param> /// <param name="encoding">The encoding to use.</param>
/// <returns>Returns a <see cref="Byte"/>[].</returns> /// <returns>Returns a <see cref="Byte"/>[].</returns>
public static byte[] GetBytes(this string str, Encoding encoding) => public static byte[] GetBytes(this string str, Encoding encoding)
encoding.GetBytes(str); {
return encoding.GetBytes(str);
}
/// <summary> /// <summary>
/// Repeats a string a specified number of times. /// Repeats a string a specified number of times.
@ -135,7 +145,9 @@
/// <param name="str">The <see cref="SecureString"/> to convert.</param> /// <param name="str">The <see cref="SecureString"/> to convert.</param>
/// <param name="extension">Whether or not to use this extension method.</param> /// <param name="extension">Whether or not to use this extension method.</param>
/// <returns>Returns a <see cref="String"/>.</returns> /// <returns>Returns a <see cref="String"/>.</returns>
public static string ToString(this SecureString str, bool extension) => public static string ToString(this SecureString str, bool extension)
extension ? (new NetworkCredential(String.Empty, str).Password) : str.ToString(); {
return extension ? (new NetworkCredential(String.Empty, str).Password) : str.ToString();
}
} }
} }