A collection of useful extension methods for .NET types including strings, collections, dictionaries, enumerables, and reflection.
$ dotnet add package ktsu.ExtensionsA comprehensive utility library of extension methods for collections, strings, dictionaries, and reflection in .NET.
ktsu.Extensions is a utility library that enhances the functionality of standard .NET types through extension methods. It provides a wide range of utilities for batch operations, string manipulations, and reflection helpers, making it easier to work with common data structures and types in a consistent, null-safe manner.
Enumerable Extensions
WithIndex: Enumerates over an enumerable with the index of the itemToCollection: Converts an enumerable to a collectionForEach: Applies an action to each element of an enumerableAnyNull: Checks if the enumerable contains any null itemsJoin: Concatenates elements with a separatorToStringEnumerable: Converts items to strings with null handlingWriteItemsToConsole: Outputs collection items to the consoleCollection Extensions
AddFrom: Adds items from an enumerable to a collectionReplaceWith: Replaces all items in a collection with items from an enumerableDictionary Extensions
GetOrCreate: Gets the value for a key or creates a new value if the key doesn't existAddOrReplace: Adds a new value or replaces an existing valueString Extensions
StartsWithOrdinal, EndsWithOrdinal, ContainsOrdinal)RemoveSuffix, RemovePrefix)ReplaceOrdinal: Replaces text using ordinal comparisonDetermineLineEndings, NormalizeLineEndings)Reflection Extensions
TryFindMethod: Searches for methods across inheritance hierarchiesInstall-Package ktsu.Extensions
dotnet add package ktsu.Extensions
<PackageReference Include="ktsu.Extensions" Version="x.y.z" />
using ktsu.Extensions;
// Iterate with index
foreach (var (item, index) in myList.WithIndex())
{
Console.WriteLine($"Item at position {index}: {item}");
}
// Apply action to each item
myList.ForEach(item => Console.WriteLine(item));
// Check for nulls
if (myList.AnyNull())
{
Console.WriteLine("List contains null items");
}
// Join items with a separator
var items = new[] { "apple", "banana", "cherry" };
string joined = items.Join(", "); // "apple, banana, cherry"
// Convert to string enumerable
var numbers = new[] { 1, 2, 3 };
var strings = numbers.ToStringEnumerable(); // ["1", "2", "3"]
using ktsu.Extensions;
string text = "Hello, World!";
// Ordinal string comparisons
if (text.StartsWithOrdinal("Hello"))
{
Console.WriteLine("Text starts with 'Hello'");
}
// Prefix/suffix manipulation
string withoutPrefix = text.RemovePrefix("Hello, "); // "World!"
string withoutSuffix = text.RemoveSuffix("!"); // "Hello, World"
// Line ending handling
string mixedText = "Line1\r\nLine2\nLine3";
var lineEndingStyle = mixedText.DetermineLineEndings(); // LineEndingStyle.Mixed
string normalized = mixedText.NormalizeLineEndings(LineEndingStyle.Unix); // All \n
using ktsu.Extensions;
var cache = new Dictionary<string, List<string>>();
// Get or create a value (uses parameterless constructor)
var items = cache.GetOrCreate("key");
items.Add("item1");
// Get or create with a specific default value
var otherItems = cache.GetOrCreate("key2", new List<string> { "default" });
// Add or replace a value
cache.AddOrReplace("key3", new List<string> { "item2" });
using ktsu.Extensions;
var collection = new List<string>();
// Add multiple items at once
collection.AddFrom(new[] { "item1", "item2", "item3" });
// Replace all items in the collection
collection.ReplaceWith(new[] { "new1", "new2" }); // Collection now contains only "new1" and "new2"
using ktsu.Extensions;
var items = new[] { "one", null, "three" };
// Convert to strings with null handling
var strings1 = items.ToStringEnumerable(NullItemHandling.Remove); // ["one", "three"]
var strings2 = items.ToStringEnumerable(NullItemHandling.Include); // ["one", null, "three"]
// NullItemHandling.Throw will throw an exception if null items are found
// Join with null handling
var joined = items.Join(", ", NullItemHandling.Remove); // "one, three"
using ktsu.Extensions;
using System.Reflection;
// Find a method across inheritance hierarchy
if (someType.TryFindMethod("MethodName", BindingFlags.Instance | BindingFlags.Public, out var methodInfo))
{
// Use the method info
methodInfo.Invoke(instance, parameters);
}
| Method | Description |
|---|---|
WithIndex | Enumerates with the index of each item |
ToCollection | Converts an enumerable to a collection |
ForEach | Applies an action to each element |
AnyNull | Checks if enumerable contains any null items |
Join | Concatenates elements with a separator |
ToStringEnumerable | Converts items to strings with null handling |
WriteItemsToConsole | Displays enumerable items in console |
| Method | Description |
|---|---|
StartsWithOrdinal | Checks if string starts with value using ordinal comparison |
EndsWithOrdinal | Checks if string ends with value using ordinal comparison |
ContainsOrdinal | Checks if string contains value using ordinal comparison |
RemovePrefix | Removes a prefix from a string if present |
RemoveSuffix | Removes a suffix from a string if present |
ReplaceOrdinal | Replaces text using ordinal comparison |
DetermineLineEndings | Identifies line ending style in a string |
NormalizeLineEndings | Converts line endings to a specific style |
| Method | Description |
|---|---|
AddFrom | Adds items from an enumerable to a collection |
ReplaceWith | Replaces all items in a collection with new items |
| Method | Description |
|---|---|
GetOrCreate | Gets existing value or creates new one |
AddOrReplace | Adds a new value or replaces existing one |
Contributions are welcome! Here's how you can help:
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Please make sure to update tests as appropriate.
This project is licensed under the MIT License - see the LICENSE.md file for details.