Bodde.Common.Extensions
1.4.0
Prefix Reserved
dotnet add package Bodde.Common.Extensions --version 1.4.0
NuGet\Install-Package Bodde.Common.Extensions -Version 1.4.0
<PackageReference Include="Bodde.Common.Extensions" Version="1.4.0" />
<PackageVersion Include="Bodde.Common.Extensions" Version="1.4.0" />
<PackageReference Include="Bodde.Common.Extensions" />
paket add Bodde.Common.Extensions --version 1.4.0
#r "nuget: Bodde.Common.Extensions, 1.4.0"
#:package Bodde.Common.Extensions@1.4.0
#addin nuget:?package=Bodde.Common.Extensions&version=1.4.0
#tool nuget:?package=Bodde.Common.Extensions&version=1.4.0
Bodde.Common.Extensions
This package contains a collection of lightweight extension methods for the most common uses. It does not depend on any other package.
Getting Started
Install the package using the .NET CLI:
dotnet add package Bodde.Common.Extensions
Then add the following using statement to your C# code:
using Bodde.Common.Extensions;
Projects reference
| Project | Description | GitHub |
|---|---|---|
Bodde.Common.Extensions |
Main library containing the extension methods. | View project |
Bodde.Common.Extensions.Test |
Automated tests for the library. | View project |
Samples.ConsoleApp |
Console application demonstrating package usage. | View project |
API Reference
| Class | Method | Description |
|---|---|---|
T[]? |
IsNullOrEmpty<T> |
Determines whether an array is null or contains no elements. |
T[]? |
IsNotNullOrEmpty<T> |
Determines whether an array is not null and contains at least one element. |
T[]? |
OrEmpty<T> |
Returns the original array, or an empty array when the value is null. |
T[] |
IsEmpty<T> |
Determines whether an array contains no elements. |
IEnumerable<T> |
ToCsv<T> |
Converts a sequence of values to a CSV-formatted string. |
IEnumerable<T> |
ToCsv<T> with formatter |
Converts a sequence to CSV using a custom function to format each value. |
string |
FromCsv |
Splits a CSV-formatted string into an array of strings. |
string |
FromCsv<T> |
Converts a CSV-formatted string into an array of values implementing IConvertible. |
string |
FromCsv<T> with parser |
Converts each CSV value with a custom parser function. |
IEnumerable<T> |
FormatAsTable<T> |
Formats a sequence as a text table. |
string? |
IsNullOrEmpty |
Determines whether a string is null or empty. |
string? |
IsNullOrWhiteSpace |
Determines whether a string is null, empty, or contains only whitespace characters. |
string? |
IsNotNullOrEmpty |
Determines whether a string is not null and not empty. |
string? |
IsNotNullOrWhiteSpace |
Determines whether a string is not null and contains at least one non-whitespace character. |
string? |
OrEmpty |
Returns an empty string when the value is null; otherwise, returns the original string. |
string |
IsEmpty |
Determines whether a string is empty. |
string |
IsEmptyOrWhiteSpace |
Determines whether a string is empty or contains only whitespace characters. |
string |
IsCapitalized |
Determines whether the first character of a string is uppercase. |
string |
Capitalize |
Converts the first character of a string to uppercase. |
string |
Uncapitalize |
Converts the first character of a string to lowercase. |
string |
EncloseIn |
Encloses a string with the same text on both sides. |
string |
EncloseIn (left and right) |
Encloses a string with separate left and right values. |
string |
DoubleQuote |
Encloses a string in double quotes. |
string |
SingleQuote |
Encloses a string in single quotes. |
string |
Parenthesize |
Encloses a string in parentheses. |
string |
SquareBracketize |
Encloses a string in square brackets. |
string |
CurlyBracketize |
Encloses a string in curly brackets. |
string |
Pluralize |
Returns a basic plural form of a word, including common irregular plurals. |
string |
Hyphenize |
Converts a string to kebab-case. |
string |
Dehyphenize |
Removes hyphens and capitalizes the character following each hyphen. |
string |
Tokenize by character |
Splits a string into tokens using a character separator. |
string |
Tokenize by string |
Splits a string into tokens using a string separator. |
string |
ConvertTo<T> |
Converts a string to a supported target type using invariant culture. |
string |
ConvertTo(Type) |
Converts a string to a supported target type using invariant culture. |
Regex |
GetMatchValues |
Gets the matched text for every match in the input string. |
Regex |
GetGroupValues (all groups) |
Gets the captured values for every group, excluding the complete match group. |
Regex |
GetGroupValues |
Gets all successful captures for a named group across every match in the input text. |
MatchCollection |
GetMatchValues |
Gets the matched text for each regular expression match. |
MatchCollection |
GetGroupValues (all groups) |
Gets the captured values for every group, excluding the complete match group. |
MatchCollection |
GetGroupValues |
Gets all successful captures for a named group from a collection of matches. |
Type |
IsNullable |
Determines whether a type can contain a null value. |
Type |
IsNumeric |
Determines whether a type is one of the supported numeric types. |
Type |
IsCollection |
Determines whether a type represents a collection, excluding string. |
Type |
GetPropertyInfos |
Gets the properties matching the specified reflection binding flags. |
Type |
GetPropertyNames |
Gets the names of properties matching the specified reflection binding flags. |
T[]?.IsNullOrEmpty<T>
Determines whether an array is null or contains no elements.
Return type: bool - true when the array is null or empty; otherwise, false.
int[]? values = null;
var isNullOrEmpty = values.IsNullOrEmpty(); // true
T[]?.IsNotNullOrEmpty<T>
Determines whether an array is not null and contains at least one element.
Return type: bool - true when the array is not null and not empty; otherwise, false.
int[]? values = [1, 2, 3];
var hasValues = values.IsNotNullOrEmpty(); // true
T[]?.OrEmpty<T>
Returns the original array, or an empty array when the value is null.
Return type: T[] - The original array, or an empty array when the value is null.
int[]? values = null;
var safeValues = values.OrEmpty(); // []
T[].IsEmpty<T>
Determines whether an array contains no elements.
Return type: bool - true when the array is empty; otherwise, false.
var values = Array.Empty<int>();
var isEmpty = values.IsEmpty(); // true
IEnumerable<T>.ToCsv<T>
Converts a sequence of values to a CSV-formatted string.
| Parameter | Type | Default | Description |
|---|---|---|---|
separator |
string |
"," |
The separator placed between values. |
Return type: string - The sequence formatted as a CSV string.
var values = new[] { "A", "B", "C" };
var csv = values.ToCsv(); // A,B,C
var semicolonCsv = values.ToCsv("; "); // A; B; C
IEnumerable<T>.ToCsv<T> (with formatter)
Converts a sequence to CSV using a custom function to format each value.
| Parameter | Type | Default | Description |
|---|---|---|---|
formatter |
Func<T, string> |
Required | Converts each value to a string. |
separator |
string |
"," |
The separator placed between converted values. |
Return type: string - The formatted values joined into a CSV string.
var employees = new[] { new { Name = "John", Age = 35 } };
var csv = employees.ToCsv(employee => $"{employee.Name} ({employee.Age})");
// John (35)
string.FromCsv
Splits a CSV-formatted string into an array of strings.
| Parameter | Type | Default | Description |
|---|---|---|---|
separator |
string |
"," |
The separator between values. |
trim |
bool |
true |
Removes surrounding whitespace from each value. |
removeEmpty |
bool |
false |
Removes empty values from the result. |
Return type: string[] - The values extracted from the CSV string.
var values = "A, B, C".FromCsv(); // ["A", "B", "C"]
string.FromCsv<T>
Converts a CSV-formatted string into an array of values implementing IConvertible.
| Parameter | Type | Default | Description |
|---|---|---|---|
separator |
string |
"," |
The separator between values. |
trim |
bool |
true |
Removes surrounding whitespace from each value. |
removeEmpty |
bool |
false |
Removes empty values from the result. |
Return type: T[] - The CSV values converted to T.
var values = "10, 20, 30".FromCsv<int>(); // [10, 20, 30]
string.FromCsv<T> (with parser)
Converts each CSV value with a custom parser function.
| Parameter | Type | Default | Description |
|---|---|---|---|
parser |
Func<string, T> |
Required | Converts each string value to T. |
separator |
string |
"," |
The separator between values. |
trim |
bool |
true |
Removes surrounding whitespace from each value. |
removeEmpty |
bool |
false |
Removes empty values from the result. |
Return type: T[] - The CSV values converted by the parser.
var values = "yes,no".FromCsv(value => value == "yes"); // [true, false]
IEnumerable<T>.FormatAsTable<T>
Formats a sequence as a text table. When no columns are supplied, public properties of the item type are used automatically.
| Parameter | Type | Description |
|---|---|---|
columns |
params FormatTableColumn<T>[] |
Optional column definitions. |
Return type: string - The sequence formatted as a text table.
<br/>
FormatTableColumn<T> constructor allows to set the following parameters:
| Parameter | Type | Description |
|---|---|---|
columnSelector |
Expression<Func<T, object>> |
The expression that selects the column property. |
header |
string? |
The column header, or the selected property path when omitted. Defaults to null. |
rightAlign |
bool? |
Indicates whether values should be right-aligned. Defaults to automatic alignment based on the property type. |
formatter |
Func<object?, string>? |
The function used to format column values. Defaults to null. |
record Employee(string Name, string role, int Age);
var employees = new[]
{
new Employee("John", "Manager", 35),
new Employee("Maria", "Developer", 29)
};
var table = employees.FormatAsTable();
/*
Name Role Age
-------------------
John Manager 35
Maria Developer 29
*/
var tableWithColumns = employees.FormatAsTable([
new(_ => _.Name, "Employee"),
new(_ => _.Age, "Age", rightAlign: true)
]);
/*
Employee Age
-------------
John 35
Maria 29
*/
string?.IsNullOrEmpty
Determines whether a string is null or empty.
Return type: bool - true when the string is null or empty; otherwise, false.
string? value = null;
var result = value.IsNullOrEmpty(); // true
string?.IsNullOrWhiteSpace
Determines whether a string is null, empty, or contains only whitespace characters.
Return type: bool - true when the string is null, empty, or whitespace; otherwise, false.
var result = " \t".IsNullOrWhiteSpace(); // true
string?.IsNotNullOrEmpty
Determines whether a string is not null and not empty.
Return type: bool - true when the string is not null and not empty; otherwise, false.
string? value = "Hello";
var result = value.IsNotNullOrEmpty(); // true
string?.IsNotNullOrWhiteSpace
Determines whether a string is not null and contains at least one non-whitespace character.
Return type: bool - true when the string is not null and contains at least one non-whitespace character; otherwise, false.
string? value = "Hello";
var result = value.IsNotNullOrWhiteSpace(); // true
string?.OrEmpty
Returns an empty string when the value is null; otherwise, returns the original string.
Return type: string - string.Empty when the value is null; otherwise, the original string.
string? value = null;
var result = value.OrEmpty(); // ""
string.IsEmpty
Determines whether a string is empty.
Return type: bool - true when the string is empty; otherwise, false.
var result = "".IsEmpty(); // true
string.IsEmptyOrWhiteSpace
Determines whether a string is empty or contains only whitespace characters.
Return type: bool - true when the string is empty or whitespace; otherwise, false.
var result = " ".IsEmptyOrWhiteSpace(); // true
string.IsCapitalized
Determines whether the first character of a string is uppercase. An empty string returns false.
Return type: bool - true when the first character is uppercase; otherwise, false.
var result = "Hello".IsCapitalized(); // true
string.Capitalize
Converts the first character of a string to uppercase. null and empty values are returned unchanged.
Return type: string - The string with its first character converted to uppercase.
var result = "hello".Capitalize(); // Hello
string.Uncapitalize
Converts the first character of a string to lowercase. null and empty values are returned unchanged.
Return type: string - The string with its first character converted to lowercase.
var result = "Hello".Uncapitalize(); // hello
string.EncloseIn
Encloses a string with the same text on both sides.
| Parameter | Type | Description |
|---|---|---|
text |
string |
The text placed before and after the string. |
Return type: string - The string enclosed with the specified text.
var result = "Hello".EncloseIn("*"); // *Hello*
string.EncloseIn (left and right)
Encloses a string with separate values on the left and right sides.
| Parameter | Type | Description |
|---|---|---|
left |
string |
The text placed before the string. |
right |
string |
The text placed after the string. |
Return type: string - The string enclosed with the specified left and right values.
var result = "Hello".EncloseIn("<", ">"); // <Hello>
string.DoubleQuote
Encloses a string in double quotes.
Return type: string - The string enclosed in double quotes.
var result = "Hello".DoubleQuote(); // "Hello"
string.SingleQuote
Encloses a string in single quotes.
Return type: string - The string enclosed in single quotes.
var result = "Hello".SingleQuote(); // 'Hello'
string.Parenthesize
Encloses a string in parentheses.
Return type: string - The string enclosed in parentheses.
var result = "Hello".Parenthesize(); // (Hello)
string.SquareBracketize
Encloses a string in square brackets.
Return type: string - The string enclosed in square brackets.
var result = "Hello".SquareBracketize(); // [Hello]
string.CurlyBracketize
Encloses a string in curly brackets.
Return type: string - The string enclosed in curly brackets.
var result = "Hello".CurlyBracketize(); // {Hello}
string.Pluralize
Returns a basic plural form of a word, including a set of common irregular plurals, while preserving the input capitalization style.
Return type: string - The pluralized word.
var regular = "box".Pluralize(); // boxes
var irregular = "Child".Pluralize(); // Children
string.Hyphenize
Converts a string to kebab-case by inserting hyphens before uppercase characters and replacing spaces with hyphens.
Return type: string - The string converted to kebab-case.
var result = "HelloWorld".Hyphenize(); // hello-world
string.Dehyphenize
Removes hyphens and capitalizes the character following each hyphen.
Return type: string - The string with hyphens removed and following characters capitalized.
var result = "hello-world".Dehyphenize(); // helloWorld
string.Tokenize (by character)
Splits a string into tokens using a character separator.
| Parameter | Type | Default | Description |
|---|---|---|---|
separator |
char |
Required | The character separating tokens. |
trim |
bool |
false |
Removes surrounding whitespace from each token. |
removeEmpty |
bool |
false |
Removes empty tokens from the result. |
Return type: string[] - The tokens extracted from the string.
var values = "A | B | C".Tokenize('|', trim: true); // ["A", "B", "C"]
string.Tokenize (by string)
Splits a string into tokens using a string separator. An empty separator throws ArgumentOutOfRangeException.
| Parameter | Type | Default | Description |
|---|---|---|---|
separator |
string |
Required | The string separating tokens. |
trim |
bool |
false |
Removes surrounding whitespace from each token. |
removeEmpty |
bool |
false |
Removes empty tokens from the result. |
Return type: string[] - The tokens extracted from the string.
var values = "A<->B<->C".Tokenize("<->"); // ["A", "B", "C"]
string.ConvertTo<T>
Converts the string to the specified target type using CultureInfo.InvariantCulture.
Supported target types include types implementing IConvertible, enumerations, TimeSpan, DateTime, DateTimeOffset, and their nullable forms.
Return type: T - The converted value.
var integer = "10".ConvertTo<int>(); // 10
var decimalValue = "3.14".ConvertTo<decimal>(); // 3.14
var role = "Senior".ConvertTo<RoleType>(); // RoleType.Senior
var duration = "2:15:30".ConvertTo<TimeSpan>(); // 02:15:30
string.ConvertTo(Type)
Converts the string to the specified target type using CultureInfo.InvariantCulture.
Supported target types include types implementing IConvertible, enumerations, TimeSpan, DateTime, DateTimeOffset, and their nullable forms.
| Parameter | Type | Description |
|---|---|---|
targetType |
Type |
The target type. |
Return type: object - The converted value.
var integer = "10".ConvertTo(typeof(int)); // 10
var date = "2009-06-15T13:45:30Z".ConvertTo(typeof(DateTimeOffset)); // 15/06/2009 13:45:30 +00:00
Regex.GetMatchValues
Gets the matched text for every match in the input string.
| Parameter | Type | Default | Description |
|---|---|---|---|
input |
string |
Required | The input text to search. |
Return type: string[] - The matched text for each regular expression match.
var regex = new Regex(@"(\w+)");
var values = regex.GetMatchValues("Call me Sarah");
// ["Call", "me", "Sarah"]
Regex.GetGroupValues (all groups)
Gets the captured values for every group in every match. The first group of each match is skipped because it represents the complete match.
| Parameter | Type | Default | Description |
|---|---|---|---|
input |
string |
Required | The input text to search. |
Return type: string[] - The captured values for each group, excluding the first group of each match, which represents the complete match.
var regex = new Regex(@"(\w+)-(\w+)");
var values = regex.GetGroupValues("one-two three-four");
// ["one", "two", "three", "four"]
Regex.GetGroupValues
Gets all successful captures for the specified named group across every match in the input text.
| Parameter | Type | Default | Description |
|---|---|---|---|
input |
string |
Required | The input text to search. |
groupName |
string |
Required | The name of the capturing group to extract. |
Return type: string[] - The values captured for the named group.
var pattern = @"(?<name>\w+)";
var input = "Call me Sarah, don't call me Sally!";
var values = new Regex(pattern).GetGroupValues(input, "name");
// ["Sarah", "Sally"]
MatchCollection.GetMatchValues
Gets the matched text for each regular expression match.
Return type: string[] - The matched text for each match.
var matches = new Regex(@"(\w+)").Matches("Call me Sarah");
var values = matches.GetMatchValues();
// ["Call", "me", "Sarah"]
MatchCollection.GetGroupValues (all groups)
Gets the captured values for every group in every match. The first group of each match is skipped because it represents the complete match.
Return type: string[] - The captured values for each group, excluding the first group of each match, which represents the complete match.
var matches = new Regex(@"(\w+)-(\w+)").Matches("one-two three-four");
var values = matches.GetGroupValues();
// ["one", "two", "three", "four"]
MatchCollection.GetGroupValues
Gets all successful captures for the specified named group from a collection of matches.
| Parameter | Type | Default | Description |
|---|---|---|---|
groupName |
string |
Required | The name of the capturing group to extract. |
Return type: string[] - The values captured for the named group.
var regex = new Regex("(?<verb>\w+)\s+(?<pronoun>\w+)\s(?<name>\w+)\.");
var matches = regex.Matches("Call me Sally.")
var verb = matches.GetGroupValues("verb").FirstOrDefault(); // "Call"
var pronoun = matches.GetGroupValues("pronoun").FirstOrDefault(); // "me"
var name = matches.GetGroupValues("name").FirstOrDefault(); // "Sally"
Type.IsNullable
Determines whether a type can contain a null value. Reference types and nullable value types return true.
Return type: bool - true when the type is a reference type or nullable value type; otherwise, false.
var result = typeof(int?).IsNullable(); // true
Type.IsNumeric
Determines whether a type is one of the supported numeric types, including nullable numeric types.
Return type: bool - true when the type is a supported numeric type; otherwise, false.
var result = typeof(decimal).IsNumeric(); // true
Type.IsCollection
Determines whether a type represents a collection by checking whether it implements IEnumerable. Strings are excluded from the result.
Return type: bool - true when the type implements IEnumerable and is not string; otherwise, false.
var arrayResult = typeof(int[]).IsCollection(); // true
var stringResult = typeof(string).IsCollection(); // false
Type.GetPropertyInfos
Gets the properties matching the specified reflection binding flags. Results can be cached by type and flags.
| Parameter | Type | Default | Description |
|---|---|---|---|
useCache |
bool |
true |
Indicates whether cached property information may be used. |
flags |
BindingFlags |
Public \| Instance \| GetProperty |
The binding flags used to find properties. |
Return type: PropertyInfo[] - The properties matching the specified binding flags.
var properties = typeof(Employee).GetPropertyInfos();
Type.GetPropertyNames
Gets the names of properties matching the specified reflection binding flags.
| Parameter | Type | Default | Description |
|---|---|---|---|
useCache |
bool |
true |
Indicates whether cached property information may be used. |
flags |
BindingFlags |
Public \| Instance \| GetProperty |
The binding flags used to find properties. |
Return type: string[] - The names of properties matching the specified binding flags.
var propertyNames = typeof(Employee).GetPropertyNames(); // ["Id", "Name", ...]
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Bodde.Common.Extensions:
| Package | Downloads |
|---|---|
|
Bodde.Query.OData
Provides reusable support for filtering, sorting, and paging IQueryable collections with OData syntax. |
GitHub repositories
This package is not used by any popular GitHub repositories.