# C# snippets

> Small C# methods for collections, strings, maths and dates, each with a short explanation and an example call.

Canonical: https://www.wiki.jodisand.me/snippets/csharp/
Reviewed: 2026-09-24
Related: [System design](https://www.wiki.jodisand.me/design/index.md), [Testing](https://www.wiki.jodisand.me/testing/index.md)


Short third-party examples with no argument validation or error handling. Many duplicate LINQ or BCL methods, so check `System.Linq` and the framework before copying one, and add tests for the edge cases you rely on.


## Pages

- [All](https://www.wiki.jodisand.me/snippets/csharp/all/index.md): Returns true if the provided predicate function returns true for all elements in a collection, false otherwise.
- [AverageBy](https://www.wiki.jodisand.me/snippets/csharp/averageby/index.md): Returns the average of a collection, after mapping each element to a value using the provided function.
- [Bifurcate](https://www.wiki.jodisand.me/snippets/csharp/bifurcate/index.md): Splits values into two groups. If an element in filter is true, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
- [BifurcateBy](https://www.wiki.jodisand.me/snippets/csharp/bifurcateby/index.md): Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group.
- [ByteArrayToHex](https://www.wiki.jodisand.me/snippets/csharp/bytearraytohex/index.md): Converts a byte array to its hexadecimal string representation.
- [Capitalize](https://www.wiki.jodisand.me/snippets/csharp/capitalize/index.md): Capitalizes the first letter of a string.
- [Chunk](https://www.wiki.jodisand.me/snippets/csharp/chunk/index.md): Chunks a collection into smaller lists of a specified size.
- [CompactWhitespace](https://www.wiki.jodisand.me/snippets/csharp/compactwhitespace/index.md): Returns a string with whitespaces compacted.
- [CountBy](https://www.wiki.jodisand.me/snippets/csharp/countby/index.md): Groups the elements of a collection based on the given function and returns the count of elements in each group.
- [CountOccurences](https://www.wiki.jodisand.me/snippets/csharp/countoccurences/index.md): Counts the occurences of a value in a collection.
- [DayOfTheWeek](https://www.wiki.jodisand.me/snippets/csharp/dayoftheweek/index.md): Returns the string representation of the weekday for the given DateTime.
- [Decapitalize](https://www.wiki.jodisand.me/snippets/csharp/decapitalize/index.md): Decapitalizes the first letter of a string.
- [Difference](https://www.wiki.jodisand.me/snippets/csharp/difference/index.md): Returns the difference betweend two collections.
- [DifferenceBy](https://www.wiki.jodisand.me/snippets/csharp/differenceby/index.md): Returns the difference between two collections, after applying the provided function to each element of both.
- [DistinctValues](https://www.wiki.jodisand.me/snippets/csharp/distinctvalues/index.md): Returns all distinct values in a collection.
- [DuplicateValues](https://www.wiki.jodisand.me/snippets/csharp/duplicatevalues/index.md): Returns all distinct values in a collection.
- [Fibonacci](https://www.wiki.jodisand.me/snippets/csharp/fibonacci/index.md): Generates an array, containing the Fibonacci sequence, up until the nth term.
- [FilterString](https://www.wiki.jodisand.me/snippets/csharp/filterstring/index.md): Filter a string’s contents to include only alphanumeric and allowed characters.
- [FindFirstBy](https://www.wiki.jodisand.me/snippets/csharp/findfirstby/index.md): Returns the first element in a collection that matches the given predicate function, match.
- [FindIndexOfAll](https://www.wiki.jodisand.me/snippets/csharp/findindexofall/index.md): Returns all indices in an IList that match the given predicate function, match.
- [FindIndexOfFirstBy](https://www.wiki.jodisand.me/snippets/csharp/findindexoffirstby/index.md): Returns the first index in an IList that matches the given predicate function, match.
- [FindIndexOfLastBy](https://www.wiki.jodisand.me/snippets/csharp/findindexoflastby/index.md): Returns the last index in an IList that matches the given predicate function, match.
- [FindLastBy](https://www.wiki.jodisand.me/snippets/csharp/findlastby/index.md): Returns the last element in a collection that matches the given predicate function, match.
- [FindParityOutliers](https://www.wiki.jodisand.me/snippets/csharp/findparityoutliers/index.md): Given a collection, returns the items that are parity outliers.
- [Flatten](https://www.wiki.jodisand.me/snippets/csharp/flatten/index.md): Flattens a 2D collection into a single dimension.
- [FormatDuration](https://www.wiki.jodisand.me/snippets/csharp/formatduration/index.md): Returns the human readable format of the given number of seconds.
- [Frequencies](https://www.wiki.jodisand.me/snippets/csharp/frequencies/index.md): Returns a Dictionary with the unique values of a collection as keys and their frequencies as the values.
- [GCD](https://www.wiki.jodisand.me/snippets/csharp/gcd/index.md): Calculates the greatest common divisor of the given numbers.
- [GetFirstN](https://www.wiki.jodisand.me/snippets/csharp/getfirstn/index.md): Returns the first n elements in a collection.
- [GetLastN](https://www.wiki.jodisand.me/snippets/csharp/getlastn/index.md): Returns the last n elements in a collection.
- [GetType](https://www.wiki.jodisand.me/snippets/csharp/gettype/index.md): Returns the type of the given object.
- [HaveSameContents](https://www.wiki.jodisand.me/snippets/csharp/havesamecontents/index.md): Returns true if two collections contain the same elements regardless of order, false otherwise.
- [Head](https://www.wiki.jodisand.me/snippets/csharp/head/index.md): Returns the head of a collection.
- [HexToByteArray](https://www.wiki.jodisand.me/snippets/csharp/hextobytearray/index.md): Converts a hexadecimal string to a byte array.
- [IndexOfAll](https://www.wiki.jodisand.me/snippets/csharp/indexofall/index.md): Returns all indices of n in an IList.
- [Initialize2DArray](https://www.wiki.jodisand.me/snippets/csharp/initialize2darray/index.md): Initializes a 2D array of the given width, height and value.
- [IsA](https://www.wiki.jodisand.me/snippets/csharp/isa/index.md): Returns true if the given object is of the specified type, false otherwise.
- [IsContainedIn](https://www.wiki.jodisand.me/snippets/csharp/iscontainedin/index.md): Returns true if the elements of the first collection are contained in the second one regardless of order, false otherwise.
- [IsDivisible](https://www.wiki.jodisand.me/snippets/csharp/isdivisible/index.md): Checks if the first numeric argument is divisible by the second one.
- [IsDouble](https://www.wiki.jodisand.me/snippets/csharp/isdouble/index.md): Returns true if the given string can be parsed into a double, false otherwise.
- [IsEven](https://www.wiki.jodisand.me/snippets/csharp/iseven/index.md): Returns true if the given number is even, false otherwise.
- [IsInteger](https://www.wiki.jodisand.me/snippets/csharp/isinteger/index.md): Returns true if the given string can be parsed into an integer, false otherwise.
- [IsLower](https://www.wiki.jodisand.me/snippets/csharp/islower/index.md): Checks if a string is lower case.
- [IsNotA](https://www.wiki.jodisand.me/snippets/csharp/isnota/index.md): Returns true if the given object is not of the specified type, false otherwise.
- [IsOdd](https://www.wiki.jodisand.me/snippets/csharp/isodd/index.md): Returns true if the given number is odd, false otherwise.
- [IsPowerOfTwo](https://www.wiki.jodisand.me/snippets/csharp/ispoweroftwo/index.md): Returns true if the given number is a power of 2, false otherwise.
- [IsUpper](https://www.wiki.jodisand.me/snippets/csharp/isupper/index.md): Checks if a string is upper case.
- [IsWeekday](https://www.wiki.jodisand.me/snippets/csharp/isweekday/index.md): Returns true if the given DateTime is a weekday, false otherwise.
- [IsWeekend](https://www.wiki.jodisand.me/snippets/csharp/isweekend/index.md): Returns true if the given DateTime is a not weekday, false otherwise.
- [KeepUpToN](https://www.wiki.jodisand.me/snippets/csharp/keepupton/index.md): Filters a collection keeping up to n occurences of each value.
- [LCM](https://www.wiki.jodisand.me/snippets/csharp/lcm/index.md): Calculates the least common multiple of the given numbers.
- [Mask](https://www.wiki.jodisand.me/snippets/csharp/mask/index.md): Replaces all but the last n characters in a string with the specified mask character.
- [MaxBy](https://www.wiki.jodisand.me/snippets/csharp/maxby/index.md): Returns the maximum of a collection, after mapping each element to a value using the provided function.
- [MaxDateTime](https://www.wiki.jodisand.me/snippets/csharp/maxdatetime/index.md): Returns the maximum of two DateTime values.
- [Median](https://www.wiki.jodisand.me/snippets/csharp/median/index.md): Finds the median of a list of numbers.
- [MinBy](https://www.wiki.jodisand.me/snippets/csharp/minby/index.md): Returns the minimum of a collection, after mapping each element to a value using the provided function.
- [MinDateTime](https://www.wiki.jodisand.me/snippets/csharp/mindatetime/index.md): Returns the minimum of two DateTime values.
- [MostFrequent](https://www.wiki.jodisand.me/snippets/csharp/mostfrequent/index.md): Returns the most frequent element of a collection.
- [None](https://www.wiki.jodisand.me/snippets/csharp/none/index.md): Returns true if the provided predicate function returns false for all elements in a collection, false otherwise.
- [PadNumber](https://www.wiki.jodisand.me/snippets/csharp/padnumber/index.md): Pads a given number to the specified length.
- [RandomDoubleInRange](https://www.wiki.jodisand.me/snippets/csharp/randomdoubleinrange/index.md): Returns a random double in the specified range.
- [RandomIntegerInRange](https://www.wiki.jodisand.me/snippets/csharp/randomintegerinrange/index.md): Returns a random integer in the specified range.
- [Repeat](https://www.wiki.jodisand.me/snippets/csharp/repeat/index.md): Creates a new string by repeating the given string n times.
- [Reverse](https://www.wiki.jodisand.me/snippets/csharp/reverse/index.md): Reverses a string.
- [Shuffle](https://www.wiki.jodisand.me/snippets/csharp/shuffle/index.md): Randomizes the order of the values of an IList, updating the original IList object.
- [SplitLines](https://www.wiki.jodisand.me/snippets/csharp/splitlines/index.md): Splits a multiline string into an array of lines.
- [SplitStringBy](https://www.wiki.jodisand.me/snippets/csharp/splitstringby/index.md): Splits a string into an array of strings using a multicharacter (string) separator.
- [Stringify](https://www.wiki.jodisand.me/snippets/csharp/stringify/index.md): Combines the elements of an enumerable object into a string.
- [Subarray](https://www.wiki.jodisand.me/snippets/csharp/subarray/index.md): Returns a subarray of the given array starting at the given index and having the specified length.
- [SumBy](https://www.wiki.jodisand.me/snippets/csharp/sumby/index.md): Returns the sum of a collection, after mapping each element to a value using the provided function.
- [Swap](https://www.wiki.jodisand.me/snippets/csharp/swap/index.md): Swaps the values of two variables of the same type.
- [SymmetricDifference](https://www.wiki.jodisand.me/snippets/csharp/symmetricdifference/index.md): Returns the symmetric difference betweend two collections.
- [SymmetricDifferenceBy](https://www.wiki.jodisand.me/snippets/csharp/symmetricdifferenceby/index.md): Returns the symmetric difference betweend two collections, after applying the provided function to each element of both.
- [Tail](https://www.wiki.jodisand.me/snippets/csharp/tail/index.md): Returns the tail of a collection.
- [ToCamelCase](https://www.wiki.jodisand.me/snippets/csharp/tocamelcase/index.md): Converts a string to camel case.
- [ToKebabCase](https://www.wiki.jodisand.me/snippets/csharp/tokebabcase/index.md): Converts a string to kebab case.
- [Tomorrow](https://www.wiki.jodisand.me/snippets/csharp/tomorrow/index.md): Returns tomorrow’s DateTime value.
- [ToSnakeCase](https://www.wiki.jodisand.me/snippets/csharp/tosnakecase/index.md): Converts a string to snake case.
- [ToTitleCase](https://www.wiki.jodisand.me/snippets/csharp/totitlecase/index.md): Converts a string to title case.
- [Yesterday](https://www.wiki.jodisand.me/snippets/csharp/yesterday/index.md): Returns yesterday’s DateTime value.
