C# snippets
Small C# methods for collections, strings, maths and dates, each with a short explanation and an example call.
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.
- AllReturns true if the provided predicate function returns true for all elements in a collection, false otherwise.
- AverageByReturns the average of a collection, after mapping each element to a value using the provided function.
- BifurcateSplits 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.
- BifurcateBySplits 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.
- ByteArrayToHexConverts a byte array to its hexadecimal string representation.
- CapitalizeCapitalizes the first letter of a string.
- ChunkChunks a collection into smaller lists of a specified size.
- CompactWhitespaceReturns a string with whitespaces compacted.
- CountByGroups the elements of a collection based on the given function and returns the count of elements in each group.
- CountOccurencesCounts the occurences of a value in a collection.
- DayOfTheWeekReturns the string representation of the weekday for the given DateTime.
- DecapitalizeDecapitalizes the first letter of a string.
- DifferenceReturns the difference betweend two collections.
- DifferenceByReturns the difference between two collections, after applying the provided function to each element of both.
- DistinctValuesReturns all distinct values in a collection.
- DuplicateValuesReturns all distinct values in a collection.
- FibonacciGenerates an array, containing the Fibonacci sequence, up until the nth term.
- FilterStringFilter a string’s contents to include only alphanumeric and allowed characters.
- FindFirstByReturns the first element in a collection that matches the given predicate function, match.
- FindIndexOfAllReturns all indices in an IList that match the given predicate function, match.
- FindIndexOfFirstByReturns the first index in an IList that matches the given predicate function, match.
- FindIndexOfLastByReturns the last index in an IList that matches the given predicate function, match.
- FindLastByReturns the last element in a collection that matches the given predicate function, match.
- FindParityOutliersGiven a collection, returns the items that are parity outliers.
- FlattenFlattens a 2D collection into a single dimension.
- FormatDurationReturns the human readable format of the given number of seconds.
- FrequenciesReturns a Dictionary with the unique values of a collection as keys and their frequencies as the values.
- GCDCalculates the greatest common divisor of the given numbers.
- GetFirstNReturns the first n elements in a collection.
- GetLastNReturns the last n elements in a collection.
- GetTypeReturns the type of the given object.
- HaveSameContentsReturns true if two collections contain the same elements regardless of order, false otherwise.
- HeadReturns the head of a collection.
- HexToByteArrayConverts a hexadecimal string to a byte array.
- IndexOfAllReturns all indices of n in an IList.
- Initialize2DArrayInitializes a 2D array of the given width, height and value.
- IsAReturns true if the given object is of the specified type, false otherwise.
- IsContainedInReturns true if the elements of the first collection are contained in the second one regardless of order, false otherwise.
- IsDivisibleChecks if the first numeric argument is divisible by the second one.
- IsDoubleReturns true if the given string can be parsed into a double, false otherwise.
- IsEvenReturns true if the given number is even, false otherwise.
- IsIntegerReturns true if the given string can be parsed into an integer, false otherwise.
- IsLowerChecks if a string is lower case.
- IsNotAReturns true if the given object is not of the specified type, false otherwise.
- IsOddReturns true if the given number is odd, false otherwise.
- IsPowerOfTwoReturns true if the given number is a power of 2, false otherwise.
- IsUpperChecks if a string is upper case.
- IsWeekdayReturns true if the given DateTime is a weekday, false otherwise.
- IsWeekendReturns true if the given DateTime is a not weekday, false otherwise.
- KeepUpToNFilters a collection keeping up to n occurences of each value.
- LCMCalculates the least common multiple of the given numbers.
- MaskReplaces all but the last n characters in a string with the specified mask character.
- MaxByReturns the maximum of a collection, after mapping each element to a value using the provided function.
- MaxDateTimeReturns the maximum of two DateTime values.
- MedianFinds the median of a list of numbers.
- MinByReturns the minimum of a collection, after mapping each element to a value using the provided function.
- MinDateTimeReturns the minimum of two DateTime values.
- MostFrequentReturns the most frequent element of a collection.
- NoneReturns true if the provided predicate function returns false for all elements in a collection, false otherwise.
- PadNumberPads a given number to the specified length.
- RandomDoubleInRangeReturns a random double in the specified range.
- RandomIntegerInRangeReturns a random integer in the specified range.
- RepeatCreates a new string by repeating the given string n times.
- ReverseReverses a string.
- ShuffleRandomizes the order of the values of an IList, updating the original IList object.
- SplitLinesSplits a multiline string into an array of lines.
- SplitStringBySplits a string into an array of strings using a multicharacter (string) separator.
- StringifyCombines the elements of an enumerable object into a string.
- SubarrayReturns a subarray of the given array starting at the given index and having the specified length.
- SumByReturns the sum of a collection, after mapping each element to a value using the provided function.
- SwapSwaps the values of two variables of the same type.
- SymmetricDifferenceReturns the symmetric difference betweend two collections.
- SymmetricDifferenceByReturns the symmetric difference betweend two collections, after applying the provided function to each element of both.
- TailReturns the tail of a collection.
- ToCamelCaseConverts a string to camel case.
- ToKebabCaseConverts a string to kebab case.
- TomorrowReturns tomorrow’s DateTime value.
- ToSnakeCaseConverts a string to snake case.
- ToTitleCaseConverts a string to title case.
- YesterdayReturns yesterday’s DateTime value.
No snippet matches that filter.