Snippets
C# snippets
Small, self-contained C# methods. Each one shows the implementation, a short explanation of how it works and an example call.
- Returns true if the provided predicate function returns true for all elements in a collection, false …
- AverageBy Returns the average of a collection, after mapping each element to a value using the provided function. Use …
- Bifurcate Splits values into two groups. If an element in filter is true, the corresponding element in the collection …
- BifurcateBy Splits values into two groups according to a predicate function, which specifies which group an element in the …
- ByteArrayToHex Converts a byte array to its hexadecimal string representation. Use BitConverter.ToString() to convert the …
- Capitalize Capitalizes the first letter of a string. Use string.ToCharArray() to convert the string to an array of char, …
- Chunk Chunks a collection into smaller lists of a specified size. Use IEnumerable.Select() to convert the given list …
- CompactWhitespace Returns a string with whitespaces compacted. Use Regex.Replace() with a regular expression to replace all …
- CountBy Groups the elements of a collection based on the given function and returns the count of elements in each …
- CountOccurences Counts the occurences of a value in a collection. Use IEnumerable.Count() in combination with …
- DayOfTheWeek Returns the string representation of the weekday for the given DateTime. Use DateTime.ToString() with an …
- Decapitalize Decapitalizes the first letter of a string. Use string.ToCharArray() to convert the string to an array of …
- Difference Returns the difference betweend two collections. Use IEnumerable.Except() to only return elements in the …
- DifferenceBy Returns the difference between two collections, after applying the provided function to each element of both. …
- DistinctValues Returns all distinct values in a collection. Use IEnumerable.Distinct() to get the distinct values in the …
- DuplicateValues Returns all distinct values in a collection. Use IEnumerable.GroupBy() to create groups for each distinct …
- Fibonacci Generates an array, containing the Fibonacci sequence, up until the nth term. Starting with 0 and 1, loop from …
- FilterString Filter a string’s contents to include only alphanumeric and allowed characters. Use string.ToCharArray() …
- FindFirstBy Returns the first element in a collection that matches the given predicate function, match. Use …
- FindIndexOfAll Returns all indices in an IList that match the given predicate function, match. Use Enumerable.Range() to …
- FindIndexOfFirstBy Returns the first index in an IList that matches the given predicate function, match. Use Enumerable.Range() …
- FindIndexOfLastBy Returns the last index in an IList that matches the given predicate function, match. Use Enumerable.Range() to …
- FindLastBy Returns the last element in a collection that matches the given predicate function, match. Use …
- FindParityOutliers Given a collection, returns the items that are parity outliers. Use IEnumerable.GroupBy() to create groups for …
- Flatten Flattens a 2D collection into a single dimension. Use IEnumerable.SelectMany() to flatten the 2D enumerable …
- FormatDuration Returns the human readable format of the given number of seconds. Use TimeSpan.FromSeconds() to convert the …
- Frequencies Returns a Dictionary with the unique values of a collection as keys and their frequencies as the values. Use …
- GCD Calculates the greatest common divisor of the given numbers. Define a GCD() function for two numbers, which …
- GetFirstN Returns the first n elements in a collection. Use IEnumerable.Count() to check if the enumerable is non-empty. …
- GetLastN Returns the last n elements in a collection. Use IEnumerable.Count() to check if the enumerable is non-empty. …
- GetType Returns the type of the given object. Use typeof() on the given object’s type. public static partial …
- HaveSameContents Returns true if two collections contain the same elements regardless of order, false otherwise. Use …
- Head Returns the head of a collection. Use IEnumerable.Count() to check if the enumerable is non-empty. Use …
- HexToByteArray Converts a hexadecimal string to a byte array. Use Enumerable.Range() in combination with string.Length to get …
- IndexOfAll Returns all indices of n in an IList. Use Enumerable.Range() to iterate over all indices in data. Use …
- Initialize2DArray Initializes a 2D array of the given width, height and value. Use Enumerable.Repeat() to repeat value width …
- IsA Returns true if the given object is of the specified type, false otherwise. Use the is operator to check if …
- IsContainedIn Returns true if the elements of the first collection are contained in the second one regardless of order, …
- IsDivisible Checks if the first numeric argument is divisible by the second one. Use the modulo operator (%) to check if …
- IsDouble Returns true if the given string can be parsed into a double, false otherwise. Return the result of calling …
- IsEven Returns true if the given number is even, false otherwise. Check whether a number is odd or even using the …
- IsInteger Returns true if the given string can be parsed into an integer, false otherwise. Return the result of calling …
- IsLower Checks if a string is lower case. Convert the given string to lower case, using string.ToLower() and compare …
- IsNotA Returns true if the given object is not of the specified type, false otherwise. Use the is operator to check …
- IsOdd Returns true if the given number is odd, false otherwise. Check whether a number is odd or even using the …
- IsPowerOfTwo Returns true if the given number is a power of 2, false otherwise. Use the bitwise binary AND operator (&) …
- IsUpper Checks if a string is upper case. Convert the given string to upper case, using string.ToUpper() and compare …
- IsWeekday Returns true if the given DateTime is a weekday, false otherwise. Use DateTime.DayOfWeek to check if the given …
- IsWeekend Returns true if the given DateTime is a not weekday, false otherwise. Use DateTime.DayOfWeek to check if the …
- KeepUpToN Filters a collection keeping up to n occurences of each value. Use IEnumerable.Distinct() in combination with …
- LCM Calculates the least common multiple of the given numbers. Define a _GCD() method that determines the greatest …
- Mask Replaces all but the last n characters in a string with the specified mask character. Use string.Substring() …
- MaxBy Returns the maximum of a collection, after mapping each element to a value using the provided function. Use …
- MaxDateTime Returns the maximum of two DateTime values. Use the conditional operator (?:) to return the maximum of the two …
- Median Finds the median of a list of numbers. Use the params keyword to accept either an array or a variable number …
- MinBy Returns the minimum of a collection, after mapping each element to a value using the provided function. Use …
- MinDateTime Returns the minimum of two DateTime values. Use the conditional operator (?:) to return the minimum of the two …
- MostFrequent Returns the most frequent element of a collection. Use IEnumerable.GroupBy() to group values by value. Use …
- None Returns true if the provided predicate function returns false for all elements in a collection, false …
- PadNumber Pads a given number to the specified length. Use Int32.ToString() with an appropriate format specifier, …
- RandomDoubleInRange Returns a random double in the specified range. Use Random.NextDouble() to generate a random value and map it …
- RandomIntegerInRange Returns a random integer in the specified range. Use Random.Next() to generate an integer in the desired …
- Repeat Creates a new string by repeating the given string n times. Use Enumerable.Repeat() to repeat s n times, …
- Reverse Reverses a string. Use string.ToCharArray() to convert the string to an array of char, Array.Reverse() to …
- Shuffle Randomizes the order of the values of an IList, updating the original IList object. Use the Fisher-Yates …
- SplitLines Splits a multiline string into an array of lines. Use string.Split() with all forms of the newline separator …
- SplitStringBy Splits a string into an array of strings using a multicharacter (string) separator. Use string.Split() with …
- Stringify Combines the elements of an enumerable object into a string. Use string.Join() to combine all elements in the …
- Subarray Returns a subarray of the given array starting at the given index and having the specified length. Use …
- SumBy Returns the sum of a collection, after mapping each element to a value using the provided function. Use …
- Swap Swaps the values of two variables of the same type. Pass both values by reference using the ref keyword, then …
- SymmetricDifference Returns the symmetric difference betweend two collections. Use IEnumerable.Except() to only return elements in …
- SymmetricDifferenceBy Returns the symmetric difference betweend two collections, after applying the provided function to each …
- Tail Returns the tail of a collection. Use IEnumerable.Count() to check if the enumerable is non-empty. Use …
- ToCamelCase Converts a string to camel case. Use Regex.Matches() with an appropriate regular expression to break the …
- ToKebabCase Converts a string to kebab case. Use Regex.Matches() with an appropriate regular expression to break the …
- Tomorrow Returns tomorrow’s DateTime value. Use DateTime.Now to get the current date, then use …
- ToSnakeCase Converts a string to snake case. Use Regex.Matches() with an appropriate regular expression to break the …
- ToTitleCase Converts a string to title case. Use Regex.Matches() with an appropriate regular expression to break the …
- Yesterday Returns yesterday’s DateTime value. Use DateTime.Now to get the current date, then use …
No snippet matches that filter.