Skip to content

Repository files navigation

CSharpHelperExtensions

A set of commonly used C# extension methods that reduce boilerplate across four focused namespaces: value checks, string manipulation, collection operations, and dictionary helpers.

.NET build status

CSharpHelperExtensions logo


Overview

CSharpHelperExtensions is a lightweight NuGet package of extension methods for values, strings, enumerables, and dictionaries — designed to cut down boilerplate you'd otherwise hand-write on every project.

Most C# codebases end up with the same small helpers rewritten over and over: null-safe string checks, safe parsing, membership tests, batching, and dictionary lookups with fallbacks. Keeping these scattered across projects means copy-pasting and re-testing the same logic repeatedly.

CSharpHelperExtensions exists to centralize these common patterns into one dependency-light, well-tested package, split across four focused namespaces so you only import what you need.


Outcome

Simplifies common development tasks through a comprehensive set of utility extensions for values, strings, collections, and dictionaries, reducing boilerplate code and improving consistency


Features

ValuesIn (SQL-style membership), IsBetween (four inclusive/exclusive modes), ToJson
Strings50+ null-safe, parsing, transformation, whitespace, comparison, prefix/suffix, and encoding helpers
EnumerablePresence checks, materialization, async projection, partitioning, batching, conditional mutation
DictionariesSafe lookup, add-if-missing, merging, bulk add, in-place filtering, read-only views
Interactive samplesFour .NET Interactive notebooks covering every namespace with runnable examples

Tech Stack

LayerTechnology
Language / RuntimeC# on net10.0
TestingxUnit + Shouldly

Installation

dotnet add package CSharpHelperExtensions

Namespaces

Import the namespace for the extensions you need:

NamespaceWhat it covers
CSharpHelperExtensions.ValuesIn, IsBetween, ToJson
CSharpHelperExtensions.StringsAll string extensions
CSharpHelperExtensions.EnumerableAll IEnumerable<T> and collection extensions
CSharpHelperExtensions.DictionariesAll IDictionary<TKey,TValue> extensions

Interactive Samples

The sample/ folder contains three .NET Interactive notebooks you can run directly in VS Code (with the Polyglot Notebooks extension) or Jupyter.

Before running any notebook, build the library so the DLL is available:

dotnet build

Each notebook loads the compiled DLL and imports the relevant namespace in its Setup cell — run that cell first, then run any section independently.

NotebookNamespaceWhat it covers
sample/value-extensions.ipynbCSharpHelperExtensions.ValuesIn, IsBetween (all four BetweenComparison modes), ToJson, and chaining examples
sample/string-extensions.ipynbCSharpHelperExtensions.StringsAll 50+ string methods grouped by category: null-safety, parsing, transformation, whitespace, comparisons, prefix/suffix, encoding, and chaining pipelines
sample/enumerable-extension.ipynbCSharpHelperExtensions.EnumerableAll collection methods: presence checks, materialization, async projection, partitioning, batching, conditional mutation, and chaining pipelines
sample/dictionary-extensions.ipynbCSharpHelperExtensions.DictionariesAll dictionary methods: safe lookup, add-if-missing, merging, bulk add, in-place filtering, read-only views, and chaining pipelines

Usage

Values

usingCSharpHelperExtensions.Values;// Membership check — like SQL IN"admin".In("admin","superadmin");// trueHttpMethod.Post.In(Post,Put,Patch);// true// Range check — inclusive by default5.IsBetween(1,10);// true1.IsBetween(1,10,BetweenComparison.ExcludeBoth);// false// JSON serialisation via Newtonsoft.Jsonnew{Name="Alice",Age=30}.ToJson();// {"Name":"Alice","Age":30}new{Name="Alice"}.ToJson(indentation:true);// pretty-printed

Strings

usingCSharpHelperExtensions.Strings;// Null-safety" ".IsNullOrEmpty();// true (checks whitespace)"hello".HasValue();// true((string)null).OrDefault("N/A");// "N/A"// Transformation" Hello World ".TrimToLower();// "hello world""café au lait".ToSlug();// "cafe-au-lait""4111111111111234".MaskStart(4);// "************1234"// Safe parsing — returns null instead of throwing"42".ToIntOrNull();// 42"abc".ToIntOrNull();// null// Comparisons"Hello".EqualsIgnoreCase("HELLO");// true"path/".EnsurePrefix("/");// "/path/""report.csv".TrimSuffix(".csv");// "report"// Encoding"Hello".Base64Encode();// "SGVsbG8=""Hello".ToBase64Url();// URL-safe, no padding chars

Enumerable

usingCSharpHelperExtensions.Enumerable;// Null-safe presence checkslist.HasAny();// non-null and non-emptylist.None();// null or emptylist.OrEmpty();// null → empty sequence// Filteringitems.WhereNotNull();// removes null elementsstrings.CleanNullOrEmptyItems();// removes null, empty, and whitespace strings// Async projection with optional concurrency capvarresults=awaitids.SelectAsync(FetchAsync,maxParallel:4);// Splittingvar(passed,failed)=scores.Partition(s =>s>=60);varbatches=items.Batch(100);// process in chunks// Conditional building — fluent, returns same listvartags=newList<string>().AddIf(isPremium,"premium").AddIf(isAdmin,"admin");// Min/Max that return default instead of throwing on emptypeople.MinByOrDefault(p =>p.Age);people.MaxByOrDefault(p =>p.Age);// Utilities42.Yield();// wrap a single value as IEnumerable<T>items.WithIndex();// (Index, Item) tuplesnames.JoinAsString(", ");// fluent string.Join

Dictionaries

usingCSharpHelperExtensions.Dictionaries;// Safe lookup — returns default instead of throwing on missing key or null dictDictionaryExtensions.GetValueOrDefault(dict,"key");// value or default(TValue)// Add-if-missing — factory only called when key is absentcache.GetOrAdd("user:1", key =>LoadFromDb(key));// existing or newly stored value// Merge two dictionaries — overwrite:false keeps existing values (default)defaults.Merge(overrides,overwrite:true);// returns same dict for chaining// Bulk add from any IEnumerable<KeyValuePair>inventory.AddRange(incomingItems);// returns same dict for chaining// Filter in-place by key predicateconfig.RemoveWhere(k =>k.StartsWith("internal."));// returns same dict for chaining// Expose as a live read-only viewIReadOnlyDictionary<string,int>view=DictionaryExtensions.AsReadOnly(dict);

Building and Testing

# Build
dotnet build
# Run all tests
dotnet test# Run tests with output
dotnet test --verbosity normal
# Run a specific test
dotnet test --filter "FullyQualifiedName~MethodName"

License

Licensed under the MIT License.

Copyright (c) 2026 Bipin Radhakrishnan (https://github.com/rbipin/CSharpHelperExtensions)

About

A set of commonly used C# extension methods that reduce boilerplate across four focused namespaces: value checks, string manipulation, collection operations, and dictionary helpers.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages