Startbit-CoreUtilities 1.0.0

dotnet add package Startbit-CoreUtilities --version 1.0.0
                    
NuGet\Install-Package Startbit-CoreUtilities -Version 1.0.0
                    
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="Startbit-CoreUtilities" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Startbit-CoreUtilities" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Startbit-CoreUtilities" />
                    
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 Startbit-CoreUtilities --version 1.0.0
                    
#r "nuget: Startbit-CoreUtilities, 1.0.0"
                    
#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 Startbit-CoreUtilities@1.0.0
                    
#: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=Startbit-CoreUtilities&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Startbit-CoreUtilities&version=1.0.0
                    
Install as a Cake Tool

Startbit-CoreUtilities

A compact collection of C# extension methods simplifying common operations for DateTime, string, numeric, and color types, including conversions, calculations, validations, formatting, color transformations, palette generation, and more.


DateTime Extensions

Methods and Usage

  • Quarter
    Returns the quarter of the year (1-4).
    int q = DateTime.Now.Quarter();

  • IsLeapYear
    Checks if a given year is a leap year.
    bool leap = 2024.IsLeapYear();

  • ToUnixTimestamp
    Converts DateTime to Unix timestamp (seconds since 1970-01-01 UTC).
    long unixTime = DateTime.Now.ToUnixTimestamp();

  • ToDateTime
    Parses an ISO 8601 string to DateTime.
    DateTime? dt = "2025-08-25T15:30:00Z".ToDateTime();

  • IsDateInRange
    Checks if a date is within a range inclusively.
    bool inRange = DateTime.Now.IsDateInRange(DateTime.Today, DateTime.Today.AddDays(7));

  • StartOfWeek
    Gets the start date of the week (default Monday).
    DateTime startOfWeek = DateTime.Now.StartOfWeek();

  • EndOfDay
    Returns the end of the day (23:59:59.9999999).
    DateTime end = DateTime.Now.EndOfDay();

  • IsWeekend
    Checks if a date falls on weekend.
    bool weekend = DateTime.Now.IsWeekend();

  • AddBusinessDays
    Adds business days, skipping weekends.
    DateTime nextBizDay = DateTime.Now.AddBusinessDays(5);

  • IsToday
    Checks if a date is today.
    bool isToday = DateTime.Now.IsToday();

  • Age
    Calculates age in years given a birthdate.
    int age = new DateTime(1990, 8, 25).Age();

  • ToUtc
    Converts date to UTC.
    DateTime utc = DateTime.Now.ToUtc();

  • ToCustomString
    Converts date to a formatted string.
    string formatted = DateTime.Now.ToCustomString("yyyy-MM-dd");

  • DateOnly
    Returns the date with time truncated.
    DateTime dateOnly = DateTime.Now.DateOnly();

  • IsWithinLastDays
    Checks if date is within the last N days.
    bool recent = DateTime.Now.AddDays(-3).IsWithinLastDays(5);


String Extensions

Methods and Usage

  • Reverse
    Reverses the characters in the string.
    var reversed = "hello".Reverse(); // olleh

  • WordCount
    Counts the number of words in the string.
    int count = "hello world".WordCount(); // 2

  • ToTitleCase
    Converts string to title case (capitalize first letter of each word).
    string title = "hello world".ToTitleCase(); // Hello World

  • RemoveDiacritics
    Removes accents and diacritics from characters.
    string normalized = "éèê".RemoveDiacritics(); // eee

  • IsAlpha / IsAlphaNumeric / IsAlphaNumericWithUnderscore
    Checks if string contains only letters, letters/digits, or letters/digits/underscore.
    bool isAlpha = "abc".IsAlpha(); // true bool isAlnum = "abc123".IsAlphaNumeric(); // true bool isAlnumUnderscore = "abc_123".IsAlphaNumericWithUnderscore(); // true

  • ToSlug
    Converts string to URL-friendly slug format.
    var slug = "Hello World!".ToSlug(); // hello-world

  • SafeSubstring / LeftCharacter / RightCharacter
    Safely extracts substrings without exceptions.
    string left = "sample".LeftCharacter(3); // sam string right = "sample".RightCharacter(3); // ple

  • CountOf
    Counts occurrences of a substring.
    int count = "banana".CountOf("an"); // 2

  • RemoveNumbers / RemoveNonLetters
    Removes digits or non-letter characters.
    string noDigits = "abc123".RemoveNumbers(); // abc string lettersOnly = "abc123!".RemoveNonLetters(); // abc

  • ToEnum<T>
    Converts string to enum value of type T.
    DayOfWeek day = "Friday".ToEnum<DayOfWeek>(); // Friday

  • Repeat
    Repeats a string N times.
    var repeated = "ha".Repeat(3); // hahaha

  • ContainsIgnoreCase
    Case-insensitive substring check.
    bool contains = "Hello".ContainsIgnoreCase("he"); // true

  • SafeTrim
    Trims whitespace safely from a string.
    string trimmed = " ".SafeTrim(); // " "

  • Truncate
    Truncates string to a maximum length.
    string truncated = "sample".Truncate(3); // sam

  • CapitalizeFirstLetter
    Capitalizes the first letter only.
    string capitalized = "hello".CapitalizeFirstLetter(); // Hello

  • ToBase64 / FromBase64
    Encode and decode base64 strings.
    string encoded = "data".ToBase64(); string decoded = encoded.FromBase64();

  • RemoveWhiteSpace
    Removes all whitespace characters.
    string noSpaces = "a b c".RemoveWhiteSpace(); // abc

  • HasDigits / HasLetters
    Checks if string contains digits or letters.
    bool hasDigits = "abc2".HasDigits(); // true bool hasLetters = "123".HasLetters(); // false

  • ToIntSafe / ToDoubleSafe
    Safely parses to int or double with default fallback.
    int i = "123".ToIntSafe(); double d = "1.23".ToDoubleSafe();

  • RemoveSpecialCharacters
    Removes all characters except letters and digits.
    string clean = "a!b@c#1".RemoveSpecialCharacters(); // abc1

  • NormalizeLineEndings
    Normalizes line endings to platform default newlines.
    string normalized = "line1\r\nline2\rline3\n".NormalizeLineEndings();

  • SplitLines
    Splits string into lines.
    string[] lines = "line1\r\nline2\nline3".SplitLines();

  • InvertCase
    Returns the string with all letters in reverse case (a->A, A->a).
    string inverted = "Hello".InvertCase(); // hELLO

  • EqualsIgnoreCase
    Returns true if the string equals another string ignoring case.
    bool equal = "hello".EqualsIgnoreCase("HELLO"); // true

  • ExtractDigits
    Extracts only digit characters from a string.
    string digits = "abc123".ExtractDigits(); // 123

  • ExtractLetters
    Extracts only letter characters from a string.
    string letters = "abc123".ExtractLetters(); // abc

  • ExtractAlphaNumeric
    Extracts only letters and digits from a string.
    string alnum = "abc123_!".ExtractAlphaNumeric(); // abc123

  • SubstringBetween
    Returns the substring between the first occurrences of two specified strings.
    string sub = "abc[start]middle[end]xyz".SubstringBetween("[start]", "[end]"); // middle

  • IsNumeric
    Returns true if the string contains only numeric characters.
    bool isNum = "12345".IsNumeric(); // true

  • IsValidEmail
    Returns true if the string is a valid email address format.
    bool isEmail = "user@example.com".IsValidEmail(); // true

  • ReplaceFirst
    Replaces the first occurrence of a substring with another substring.
    string replacedFirst = "foo bar foo".ReplaceFirst("foo", "baz"); // baz bar foo

  • ReplaceLast
    Replaces the last occurrence of a substring with another substring.
    string replacedLast = "foo bar foo".ReplaceLast("foo", "baz"); // foo bar baz

  • JoinWith
    Joins a collection of strings into a single string separated by a specified separator.
    string joined = new[] { "one", "two", "three" }.JoinWith(", "); // one, two, three


Numeric Extensions

Methods and Usage

  • IsInRange
    Checks if the integer value is within the inclusive range [min, max].
    bool check = 5.IsInRange(1, 10); // true

  • IsPositive
    Returns true if the double value is greater than zero.
    bool check = 3.14.IsPositive(); // true

  • IsNegative
    Returns true if the decimal value is less than zero.
    bool check = (-1.5m).IsNegative(); // true

  • Clamp
    Clamps the integer value to be within the inclusive range of min and max.
    int clamped = 15.Clamp(0, 10); // 10

  • ToOrdinalString
    Converts an integer to its ordinal string representation (e.g. 1 to "1st").
    string ordinal = 22.ToOrdinalString(); // "22nd"

  • IsEven
    Returns true if the integer is even.
    bool check = 4.IsEven(); // true

  • IsOdd
    Returns true if the integer is odd.
    bool check = 5.IsOdd(); // true

  • RoundToDecimalPlaces
    Rounds the double value to the specified number of decimal places.
    double rounded = 3.14159.RoundToDecimalPlaces(2); // 3.14

  • ToPercentageString
    Converts a double value to a percentage string with fixed decimals.
    string percent = 0.253.ToPercentageString(1); // "25.3%"

  • ToThousandsSeparatedString
    Formats an integer with thousand separators for readability.
    string formatted = 1234567.ToThousandsSeparatedString(); // "1,234,567"

  • IsWithinTolerance
    Checks if this double value is within the specified tolerance of another.
    bool check = 5.05.IsWithinTolerance(5, 0.1); // true

  • Factorial
    Calculates the factorial of a non-negative integer.
    long fact = 5.Factorial(); // 120

  • IsPrime
    Returns true if the integer is a prime number.
    bool check = 7.IsPrime(); // true

  • ToRomanNumeral
    Converts an integer to its Roman numeral representation (1 to 3999).
    string roman = 1999.ToRomanNumeral(); // "MCMXCIX"

  • ToByteArray
    Converts an integer to a byte array in little-endian order.
    byte[] bytes = 123.ToByteArray(); // { 123, 0, 0, 0 }


Color Extensions

Methods and Usage

  • ToRgb
    Converts a HEX string (e.g. "#ff31ca") to an RGB struct.
    var rgb = "#ff31ca".ToRgb(); // RGB { R=255, G=49, B=202 }

  • ToHex
    Converts an RGB struct to a HEX color string.
    var hex = rgb.ToHex(); // "#FF31CA"

  • ToHsl
    Converts an RGB struct to an HSL struct.
    var hsl = rgb.ToHsl(); // HSL { H=309, S=1, L=0.6 }

  • ToRgb (HSL)
    Converts an HSL struct back to an RGB struct.
    var rgb2 = hsl.ToRgb();

  • ToHsv
    Converts an RGB struct to an HSV struct.
    var hsv = rgb.ToHsv();

  • ToRgb (HSV)
    Converts an HSV struct back to RGB.
    var rgb3 = hsv.ToRgb();

  • Complementary
    Returns the complementary color for an HSL struct (180° hue shift).
    var comp = hsl.Complementary();

  • Monochromatic
    Generates a set of monochromatic HSL colors varying in lightness.
    var shades = hsl.Monochromatic(5);

  • AdjustBrightness
    Adjusts brightness of an RGB color by a factor.
    var brighter = rgb.AdjustBrightness(1.2);

  • AdjustSaturation
    Adjusts saturation of an HSL color by a factor.
    var lessSaturated = hsl.AdjustSaturation(0.7);

  • InterpolateTo
    Linearly interpolates between two RGB colors by an amount (0 to 1).
    var midColor = rgb.InterpolateTo(otherRgb, 0.5);

  • BlendWith
    Blends base RGB color with another by alpha transparency.
    var blended = rgb.BlendWith(otherRgb, 0.3);

  • ToGrayscale
    Converts an RGB color to grayscale using luminosity calculations.
    var gray = rgb.ToGrayscale();

  • Invert
    Inverts the colors of an RGB struct.
    var inverted = rgb.Invert();

  • GetLuminance
    Calculates the luminance of an RGB color for contrast analysis.
    double luminance = rgb.GetLuminance();

  • ContrastRatio
    Calculates contrast ratio between two RGB colors (1 to 21).
    double ratio = rgb.ContrastRatio(otherRgb);

  • MeetsAaContrast
    Checks if two colors meet WCAG 2.1 AA contrast (min 4.5).
    bool passes = rgb.MeetsAaContrast(otherRgb);


Enumerable Extensions

Methods and Usage

  • ChunkBy
    Splits a collection into chunks of a specified size, useful for batch processing or pagination.
    var chunks = myList.ChunkBy(10);

  • EmptyIfNull
    Returns an empty enumerable if the source is null, to safely avoid null exceptions.
    var safeEnumerable = possiblyNullEnumerable.EmptyIfNull();

  • DistinctBy
    Returns distinct elements from a collection based on a specified key selector.
    var distinctItems = myList.DistinctBy(x => x.Id);

  • ElementAtOrDefaultSafe
    Safely gets the element at the given index or a default value if out of range.
    var item = myList.ElementAtOrDefaultSafe(5);

  • ForEach
    Performs the specified action on each element in the collection.
    myList.ForEach(item => Console.WriteLine(item));

  • SelectWithIndex
    Projects each element into a new form by incorporating the element's index.
    var indexedItems = myList.SelectWithIndex((item, index) => $"{index}: {item}");

  • RandomElement
    Picks a random element from the list. Throws if the list is empty.
    var randomItem = myList.RandomElement();

  • Flatten
    Flattens a sequence of sequences into a single sequence.
    var flatList = nestedList.Flatten();


Installation

Install via NuGet:

Install-Package Startbit-CoreUtilities


Usage

Add the namespace to your code:

using Startbit-CoreUtilities;

Then call any of the extension methods on the appropriate types (DateTime, int for year extensions, string).

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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.0 255 9/2/2025

Initial release