Pixata.Extensions 2.2.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pixata.Extensions --version 2.2.2
                    
NuGet\Install-Package Pixata.Extensions -Version 2.2.2
                    
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="Pixata.Extensions" Version="2.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pixata.Extensions" Version="2.2.2" />
                    
Directory.Packages.props
<PackageReference Include="Pixata.Extensions" />
                    
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 Pixata.Extensions --version 2.2.2
                    
#r "nuget: Pixata.Extensions, 2.2.2"
                    
#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 Pixata.Extensions@2.2.2
                    
#: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=Pixata.Extensions&version=2.2.2
                    
Install as a Cake Addin
#tool nuget:?package=Pixata.Extensions&version=2.2.2
                    
Install as a Cake Tool

Pixata.Extensions Pixata.Extensions Nuget package

Pixata

Some useful utility classes and methods I've developed over the past few years. I have only put in a few so far, and the test project is woefully empty, but hopefully that will change over time.

A Nuget package is available for this project.

Here is a brief description of the methods in the classes so far...

CollectionExtensionMethods

ToObservableCollection<T>() - Converts any collection that implements IEnumerable<T> into an ObservableCollection<T>. Provides a neater syntax than passing the collection to the ObservableCollection's constructor.

ToObservableCollectionAsync<T>() - An async version of ToObservableCollection().

RemoveWhere<T>() - Removes items from an ObservableCollection based on a predicate. Mimics the List<T>.RemoveAll() method that doesn't exist for ObservableCollections

ForEach<T>() - Allows you to use ForEach on any collection that implements IEnumerable<T>, as opposed to the similarly named method built in to the .NET framework that requires you to cast the collection to a List<T> first.

Flatten<T>() - Enables you to flatten a hierarchical collection.

DateExtensionMethods

ToPrettyString() - Formats a date as "12th January 2021". This relies on the OrdinalSuffix() method in NumberExtensionMethods. Works for both non-nullable and nullable DateTime variables, returning an empty string if the value is null.

StartOfWeek() - Returns a DateTime (date part only, no time) representing the start of the week for the date passed as a parameter. Takes a DayOfWeek parameter to specify which day you consider the week to start.

StartOfMonth() - Gives you a DateTime that represents the first day of the month at 00:00:00 for the month containing the date you pass in.

EndOfMonth() - Gives you a DateTime that represents the last millisecond of the month containing the date you pass in.

EndOfDay() - Gives you a DateTime that represents the last millisecond of the day containing the date you pass in (23:59:59.999).

IsWithin() - True if the date is within the range supplied.

DateRangeToString() - Formats a date/time range in a human-friendly way, omitting redundant information. Overloads accept nullable and non-nullable from/to dates and an optional showTime boolean to include times. The formatting omits repeated parts when possible (same day, same month, same year) and produces concise strings.

EnumHelper

GetValues<T>() - Enumerates the values of an enum and returns a list of enum entries with their integer Ids and value names as string. Names are split by camel case, eg "MyEnumValue" becomes "My enum value".

ExceptionExtensions

MessageStack() - Returns the exception messages all the way down the InnerException stack. There is an overload that accepts a separator string; the default uses Environment.NewLine. MessageStack includes stack traces in the returned text.

Messages() - Similar to MessageStack but returns only the messages (no stack traces). An overload accepts a separator string; the default is Environment.NewLine.

InnerType() - Returns the type name of the innermost exception.

NumberExtensionMethods

OrdinalSuffix() - Returns the ordinal suffix, eg "st" for 1, 21, 31, etc, "nd" for 2, 22, etc, "rd" for 3, 23, etc and "th" for most other numbers. Has an optional parameter that controls whether the returned string includes the number itself (e.g. "1st") or only the suffix (e.g. "st").

DoubleToFraction() - Converts a double to a 2-tuple of its improper fractional representation, eg 3.5 is converted to (7, 2) meaning 7/2. Slightly modified from https://stackoverflow.com/a/32903747/706346.

DoubleToProperFraction() - Returns a 3-tuple representing a proper fraction, eg 3.5 is converted to (3, 1, 2) meaning 3 1/2. Note: for whole numbers the tuple may look like (5, 0, 1).

DoubleToProperFractionString() - Returns a cleaned string representation of DoubleToProperFraction(), e.g. "2/3" or "3 2/7".

ToPercentageString() - Converts a number into the string representation of it as a percentage of a maximum. There are overloads for combinations of int and double parameters; the method returns a rounded percentage string and accepts an optional digits parameter to control decimal places.

ToDurationString() - Converts a number of seconds to a human-readable duration string. For example, 125 will be converted to "2 minutes 5 seconds".

S() - Returns an empty string when the input is 1, otherwise returns "s". Useful for simple pluralisation in formatted strings (e.g. "{n} item{n.S()}").

ToFileSizeString() - Converts a byte count to a human-readable file size string (bytes, Kb, Mb, Gb, etc.) with configurable precision.

StringExtensionMethods

JoinStr() - Does the same as string.Join, but as an extension method, so it can be chained. Sample usage...

List<int> nums = [1, 2, 3];
string result = nums.JoinStr(); // result is "1, 2, 3"
string result = nums.JoinStr(", ", n => $"Number {n}"); // result is "Number 1, Number 2, Number 3"

Takes an optional separator string (default is ", ").

JoinStrAnd() - Joins the elements of a sequence into a single string, with "and" before the last element. Takes an optional function to convert each element to a string. Defaults to the element's ToString() method. Sample usage...

List<int> nums = [1, 2, 3];
string result = nums.JoinStrAnd(); // result is "1, 2 and 3"
string result = nums.JoinStrAnd(", ", n => $"Number {n}"); // result is "Number 1, Number 2 and Number 3"

SplitCamelCase() - Splits a camel case string into separate words, eg "ThisIsMyString" gets converted into "This Is My String". Very useful for working with enums (although see below for variations of this method that work directly with enums). Takes an option bool parameter that specifies whether the second and subsequent words in the returned string should be lower case. Default is true.

SplitEnumCamelCase<T>() - Splits an enum member name using camel case as the rule. For example, if you had an enum named Aminals, and it had a member named JimSpriggs, then Animals.JimSpriggs.SplitEnumCamelCase() would return "Jim Spriggs". Takes an option bool parameter as above.

SplitEnumValueCamelCase<T>() - Splits an enum member value (assumed to be an int) using camel case as the rule. Using the same enum as in the previous comment, if JimSpriggs had a value of 2, then 2.SplitEnumValueCamelCase<Animals>() would return "Jim Spriggs". Takes an option bool parameter as above.

FirstLine() - Returns the first line of a multi-line string. Useful for getting the first line of someone's address

LastLine() - Returns the last line of a multi-line string. Useful for getting the last line of someone's address

OtherLines() - Returns all but the first line of a multi-line string. Useful for getting the second and subsequent line(s) of someone's address

RemoveDiacritics() - Removes diacritics (such as �, � and �) from letters, replacing them with their (hopefully) nearest Latin equivalents. Note that for boring technical reasons, the returned string was lowercase in earlier versions of this package. Starting with version 1.27.0, case is preserved.

ToTitleCase - Replaces the first character of each word with upper case e.g. This is an example - This Is An Example. If the parameter is null, an empty string is returned

Sanitise - Sanitises a string to be safe for use as a file name. Invalid characters are replaced, and sequences of invalid characters are condensed.

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.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Pixata.Extensions:

Package Downloads
Pixata.Blazor

A set of reusable Blazor components that I found useful. Use at your own risk!

Pixata.Email

An email service for use in .NET Core projects. See the GitHub page for more information

Pixata.SimilarityChooser

A utility for finding matching elements in a collection

Pixata.AspNetCore

A set of reusable ASP.NET Core extensions that I found useful

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.2.3 266 2/23/2026
2.2.2 158 2/23/2026
2.2.0 136 2/18/2026
2.1.0 270 1/5/2026
2.0.0 497 12/15/2025
1.35.0 563 9/30/2025
1.34.0 204 9/30/2025
1.33.0 228 8/21/2025
1.32.0 321 5/28/2025
1.31.0 227 5/18/2025
1.30.0 223 5/18/2025
1.29.0 185 1/28/2025
1.28.0 275 12/1/2024
1.27.0 676 3/25/2024
1.26.0 277 1/7/2024
1.25.0 1,750 5/24/2023
1.24.0 536 3/2/2023
1.23.0 1,848 5/2/2022
1.22.0 599 5/2/2022
1.21.0 1,015 4/13/2022
Loading failed