A 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 console
Collection Extensions
AddFrom: Adds items from an enumerable to a collectionReplaceWith: Replaces all items in a collection with items from an enumerable
Dictionary 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 value
String Extensions
- Ordinal comparison helpers (
StartsWithOrdinal,EndsWithOrdinal,ContainsOrdinal) - Prefix/suffix manipulation (
RemoveSuffix,RemovePrefix) ReplaceOrdinal: Replaces text using ordinal comparison- Line ending utilities (
DetermineLineEndings,NormalizeLineEndings) NominalWordWrap: Best-effort word wrapping on word and hyphen boundaries for a given wrap width and nominal glyph width
- Ordinal comparison helpers (
Reflection Extensions
TryFindMethod: Searches for methods across inheritance hierarchies
Install-Package ktsu.Extensionsdotnet add package ktsu.Extensions<PackageReferenceInclude="ktsu.Extensions"Version="x.y.z" />usingktsu.Extensions;// Iterate with indexforeach(var(item,index)inmyList.WithIndex()){Console.WriteLine($"Item at position {index}: {item}");}// Apply action to each itemmyList.ForEach(item =>Console.WriteLine(item));// Check for nullsif(myList.AnyNull()){Console.WriteLine("List contains null items");}// Join items with a separatorvaritems=new[]{"apple","banana","cherry"};stringjoined=items.Join(", ");// "apple, banana, cherry"// Convert to string enumerablevarnumbers=new[]{1,2,3};varstrings=numbers.ToStringEnumerable();// ["1", "2", "3"]usingktsu.Extensions;stringtext="Hello, World!";// Ordinal string comparisonsif(text.StartsWithOrdinal("Hello")){Console.WriteLine("Text starts with 'Hello'");}// Prefix/suffix manipulationstringwithoutPrefix=text.RemovePrefix("Hello, ");// "World!"stringwithoutSuffix=text.RemoveSuffix("!");// "Hello, World"// Line ending handlingstringmixedText="Line1\r\nLine2\nLine3";varlineEndingStyle=mixedText.DetermineLineEndings();// LineEndingStyle.Mixedstringnormalized=mixedText.NormalizeLineEndings(LineEndingStyle.Unix);// All \n// Best-effort word wrap. Line length is wrapWidth / nominalGlyphWidth, so with a// wrap width of 110 and a nominal glyph width of 10 each line holds up to 11 chars.varlines="the quick brown fox".NominalWordWrap(wrapWidth:110f,nominalGlyphWidth:10f);// ["the quick", "brown fox"]// Existing line breaks are honored, whitespace runs collapse, and a break may fall// after a hyphen or at a soft hyphen ( renders as "-" only when it wraps).varwrapped="context-sensitive".NominalWordWrap(wrapWidth:90f,nominalGlyphWidth:10f);// ["context-", "sensitive"]usingktsu.Extensions;varcache=newDictionary<string,List<string>>();// Get or create a value (uses parameterless constructor)varitems=cache.GetOrCreate("key");items.Add("item1");// Get or create with a specific default valuevarotherItems=cache.GetOrCreate("key2",newList<string>{"default"});// Add or replace a valuecache.AddOrReplace("key3",newList<string>{"item2"});usingktsu.Extensions;varcollection=newList<string>();// Add multiple items at oncecollection.AddFrom(new[]{"item1","item2","item3"});// Replace all items in the collectioncollection.ReplaceWith(new[]{"new1","new2"});// Collection now contains only "new1" and "new2"usingktsu.Extensions;varitems=new[]{"one",null,"three"};// Convert to strings with null handlingvarstrings1=items.ToStringEnumerable(NullItemHandling.Remove);// ["one", "three"]varstrings2=items.ToStringEnumerable(NullItemHandling.Include);// ["one", null, "three"]// NullItemHandling.Throw will throw an exception if null items are found// Join with null handlingvarjoined=items.Join(", ",NullItemHandling.Remove);// "one, three"usingktsu.Extensions;usingSystem.Reflection;// Find a method across inheritance hierarchyif(someType.TryFindMethod("MethodName",BindingFlags.Instance|BindingFlags.Public,outvarmethodInfo)){// Use the method infomethodInfo.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 |
NominalWordWrap | Best-effort word wrap on word and hyphen boundaries for a wrap width and nominal glyph width |
| 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:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to update tests as appropriate.
This project is licensed under the MIT License - see the LICENSE.md file for details.