ValueCollections.Block 0.0.1-alpha

This is a prerelease version of ValueCollections.Block.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package ValueCollections.Block --version 0.0.1-alpha
NuGet\Install-Package ValueCollections.Block -Version 0.0.1-alpha
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="ValueCollections.Block" Version="0.0.1-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ValueCollections.Block --version 0.0.1-alpha
#r "nuget: ValueCollections.Block, 0.0.1-alpha"
#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.
// Install ValueCollections.Block as a Cake Addin
#addin nuget:?package=ValueCollections.Block&version=0.0.1-alpha&prerelease

// Install ValueCollections.Block as a Cake Tool
#tool nuget:?package=ValueCollections.Block&version=0.0.1-alpha&prerelease

Block is an immutable array type with structural equality. It is based on System.Collections.Immutable.ImmutableArray, a standard type in .NET.

using ValueCollections;

// Equality based on the contents
Block.Create(1, 2, 3) == Block.Create(1, 2, 3); // true

// Works inside of records
record DataBlock(int Index, Block<string> Entries);
var db0 = new DataBlock(3, Block.Create("a", "b"));
var db1 = new DataBlock(3, Block.Create("a", "b"));
db0 == db1; // true

// Works as a key in Dictionary, HashMap
// or anything that uses GetHashCode.
var dict = new Dictionary<Block<int>, string>
{
    [Block.Create(1, 2, 3)] = "Entry1"
};
dict[Block.Create(1, 2, 3)]; // "Entry1"

// Plays well with all of .NET
var items = Block.Create(1, 2, 3);
var odds = items.Where(i => i % 2 == 1);
var list = new List<int>(items);

Block is highly unstable and experimental at this stage.

Rationale

Why the name Block?

It's short and it's consistent with the equivalent planned F# feature also based on ImmutableArray.

Why do we need this?

Comparing objects for equality is common and it's becoming more common with record types. Unfortunately, all the collection types in .NET default to reference equality, which means they don't work in records.

Why should it be immutable?

Anything that supports equality should be immutable, since it can be used as keys in dictionaries and maps. In a DDD sense, this type represents a value, not an entity.

Why is this a struct?

For the same reason as ImmutableArray: reducing overhead. Since it's immutable, it doesn't matter that it's passed by value. This has the unfortunate consequence that the type has a default constructor that leaves it uninitialized, throwing NullReferenceException when you try to do anything with it except check IsDefault or IsDefaultOrEmpty. For now, I'm following ImmutableArray's design to the letter, but I'm pondering if this can be improved on.

Does this create a copy of the entire array when I use it as a function argument?

No, it copies a reference. It's the same as ImmutableArray in that regard (and almost every other).

Won't this be slow compared to T[]?

Creation, iteration, passing around and index access should be the same as regular arrays; this is an ImmutableArray which is really just a T[] under the hood. Any mutation will involve a full copy; that is O(n). For building up a collection, I would suggest ImmutableList for now, which is built to be very efficient at adding and removing elements. For memory-like access, T[] is still fine, but .NET also now has more specialized types like Span and Memory.

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. 
.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
0.0.6-alpha 151 4/18/2022
0.0.5-alpha 116 4/17/2022
0.0.4-alpha 107 4/7/2022
0.0.3-alpha 112 4/7/2022
0.0.2-alpha 122 4/7/2022
0.0.1-alpha 116 3/30/2022

Prerelease experimental version