A collection of some random C# extensions I needed in many projects.
Depending on the application, there are several ways to skin a cat.
The StartOfWeek
method returns the first day of the week using the specified date and the definition of the first day of the week.
DateTime today = new(2023, 9, 5);
DateTime startOfWeek = today.StartOfWeek();
The TakeRandom
method returns a randomly chosen item from a given array.
int[] ints = [1, 2, 3];
int i = ints.TakeRandom();
The ForEach
method iterates over an enumerable and executes an action on each element.
int hits = default;
IEnumerable<string> strings = ["a", "ab", "b", "bb"];
strings.ForEach(x => x.Contains("a"), x => hits++);
The ToRGBHexString
method turns any System.Drawing.Color
into its RGB hexadecimal string representation, with the prefix '#'.
Color color = Color.Green;
string hexString = color.ToRGBHexString();
The complete API documentation can be found here.