LS.CommonExtensions 1.0.1

dotnet add package LS.CommonExtensions --version 1.0.1
                    
NuGet\Install-Package LS.CommonExtensions -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LS.CommonExtensions" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LS.CommonExtensions" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="LS.CommonExtensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LS.CommonExtensions --version 1.0.1
                    
#r "nuget: LS.CommonExtensions, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LS.CommonExtensions@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LS.CommonExtensions&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=LS.CommonExtensions&version=1.0.1
                    
Install as a Cake Tool

LS.CommonExtensions

This library provides a set of C# extension methods packaged as a NuGet utility for everyday .NET development — especially suitable for backend, utility layers, or enterprise projects.

⚙ Main Features

1️ StringExtensions

  • SplitCamelCase: Adds spaces to split concatenated words in a PascalCase or camelCase string.
    Example:
    "HelloWorld".SplitCamelCase()"Hello World"
  • ToTitleCase: Converts all words' first letters to uppercase. Good for names and labels.
  • RemoveWhiteSpace: Removes all whitespace characters.
    Useful for comparing sanitized strings or URL keys.
  • ToSlug: Converts to lowercase, hyphenated, URL-safe format.
    Ideal for SEO slugs and clean routing paths.
  • MaskSensitive: Obscures the middle portion of a string (like IC or phone numbers).
  • ContainsIgnoreCase: Checks if one string contains another, case-insensitive.
    Helpful for flexible search logic.

2️ EnumExtensions

  • GetDisplayName: Retrieves DisplayAttribute if defined; falls back to SplitCamelCase.
  • GetIntValue: Converts an enum to its underlying int.
  • GetDisplayNameAndNumber: Combines GetIntValue and GetDisplayName.
    Example:
public enum Color { [Display(Name = "Bright Red")] Red }
Color.Red.GetDisplayNameAndNumber() // "0 (Bright Red)"

3️ DateTimeExtensions

  • ToFormattedString: Formats DateTime or nullable DateTime into a specified pattern.
    Supports ISO, 24-hour, 12-hour, Date-only, and Time-only formats.
dateTime.ToFormattedString(DateTimeExtensions.DateTimeFormatType.Iso8601);
Format Type Method Example Output Usage Context
Iso8601 now.ToFormattedString(DateTimeFormatType.Iso8601) 2025-04-15T18:30:00 API / JSON serialization
DateOnly now.ToFormattedString(DateTimeFormatType.DateOnly) 2025-04-15 UI, grid listing, DB filters
DateTime24H now.ToFormattedString(DateTimeFormatType.DateTime24H) 2025-04-15 18:30 Internal logs, system display
DateTime12H now.ToFormattedString(DateTimeFormatType.DateTime12H) 2025-04-15 06:30 PM Human-readable forms
TimeOnly now.ToFormattedString(DateTimeFormatType.TimeOnly) 18:30 Schedule pickers / compact UIs

Tip:

  • ToFormattedString gracefully handles nullable DateTime.
DateTime? nullableTime = now;
string formatted = nullableTime.ToFormattedString(DateTimeFormatType.DateTime24H);

4️ CollectionExtensions

  • AddRangeSafe: Adds multiple items while avoiding duplicates.
    Useful for merging list results or appending config.
  • RemoveRangeSafe: Removes a range safely, ignoring missing items.
    Protects against exceptions during bulk updates.
  • SyncWithKeys: Synchronises an in-memory collection from a new set of keys.
    Helps in aligning EF Core tracked items or user-modified lists.
existingItems.SyncWithKeys(newKeys, item => item.Id, key => new Item { Id = key });

5️ IntExtensions

  • IsPrime: Returns true if the number is prime. Use in filtering or quizzes.
  • IsEven / IsOdd: Checks number parity.
    Can be useful in custom sorting, alternate row behavior.
  • Clamp: Constrains a number within a min/max range.
    Ideal for score validation, paging boundaries, or form inputs.
  • ToFileSizeString: Converts byte-size to readable format like KB/MB.
    Great for storage indicators.

6️ LongExtensions

  • ToFileSizeString: Same as Int, but handles larger numbers (long).
    Perfect for streaming files, attachments, system logs.

7️ DecimalExtensions

  • ToCurrencyString: Formats to localized currency string.
    E.g., "RM1,000.00" for "ms-MY", or "$1,000.00" for "en-US".
1234.5m.ToCurrencyString(new CultureInfo("ms-MY")); // "RM1,234.50"

📦 Installation

dotnet add package LS.CommonExtensions

✅ Benefits

  • 📦 Zero-dependency, drop-in helper methods
  • 🚀 Faster utility-layer development
  • 🎯 Clean C# API: value.ExtensionMethod() style

📄 License

MIT

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 253 6/15/2025
1.0.0 193 6/15/2025