AutoCollection 0.1.21

dotnet add package AutoCollection --version 0.1.21
                    
NuGet\Install-Package AutoCollection -Version 0.1.21
                    
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="AutoCollection" Version="0.1.21">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoCollection" Version="0.1.21" />
                    
Directory.Packages.props
<PackageReference Include="AutoCollection">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 AutoCollection --version 0.1.21
                    
#r "nuget: AutoCollection, 0.1.21"
                    
#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.
#addin nuget:?package=AutoCollection&version=0.1.21
                    
Install as a Cake Addin
#tool nuget:?package=AutoCollection&version=0.1.21
                    
Install as a Cake Tool

AutoCollection

Actions NuGet
maincontinuous inspect code NuGet

A lightweight source generator library that automatically creates read-only collection wrappers for your types using the GenerateReadOnlyList attribute.

Overview

AutoCollection eliminates boilerplate code by generating IReadOnlyList<T> implementations for your collection types. This allows you to create type-safe, immutable collection wrappers without writing repetitive code.

Installation

Install the AutoCollection package via NuGet:

dotnet add package AutoCollection

Usage

AutoCollection provides a simple attribute-based API to generate read-only list implementations. There are two main ways to use this library:

1. Basic Usage with Default Implementation

Simply apply the attribute to a partial class and the source generator will create a complete implementation:

using AutoCollection;

namespace Example;

// Apply the attribute to a partial class
[GenerateReadOnlyList(typeof(string))]
public partial class SimpleStringList { }

// Usage:
var stringList = new SimpleStringList(["one", "two", "three"]);
Console.WriteLine(stringList.Count);    // Outputs: 3
Console.WriteLine(stringList[0]);       // Outputs: "one"

2. Custom Implementation with Named Backing Field

You can specify a custom backing field name and provide your own constructor:

using AutoCollection;
using System.Collections.Generic;
using System.Linq;

namespace Example;

[GenerateReadOnlyList(typeof(int), nameof(_values))]
public partial class CustomIntList
{
    public CustomIntList(IEnumerable<int> vals)
    {
        _values = vals?.ToArray() ?? throw new ArgumentNullException(nameof(vals));
    }

    private readonly int[] _values;
}

// Usage:
var intList = new CustomIntList([1, 2, 3]);
Console.WriteLine(intList.Count);    // Outputs: 3
Console.WriteLine(intList[1]);       // Outputs: 2

3. Working with Complex Types

The generator works with any type, including your own custom classes:

using AutoCollection;

namespace Example;

public class Person
{
    public string FirstName { get; set; } = "John";
    public string LastName { get; set; } = "Doe";
    public string FullName => $"{FirstName} {LastName}";
}

[GenerateReadOnlyList(typeof(Person))]
public partial class PersonList { }

// Usage:
var people = new PersonList([new Person(), new Person()]);
Console.WriteLine(people.Count);              // Outputs: 2
Console.WriteLine(people[0].FullName);        // Outputs: "John Doe"

4. Basic Usage for IList<T>

using AutoCollection;

namespace Example;

// Apply the attribute to a partial class
[GenerateList(typeof(string))]
public partial class SimpleStringList { }

// Usage:
var stringList = new SimpleStringList(["one", "two", "three"]);
Console.WriteLine(stringList.Count);    // Outputs: 3
Console.WriteLine(stringList[0]);       // Outputs: "one"
stringList.Add("Four");
Console.WriteLine(stringList[3]);       // Outputs: "four"

Features

  • Zero runtime dependencies - AutoCollection runs at build time through the C# source generator feature
  • Type safety - Get compile-time checking for your collections
  • Customizable - Use default implementation or provide your own backing field and constructor
  • Lightweight - Generates minimal code with no performance overhead

Generated Code

For a class like:

[GenerateReadOnlyList(typeof(string))]
public partial class SimpleStringList { }

The source generator creates:

public partial class SimpleStringList : IReadOnlyList<string>
{
    public SimpleStringList(IEnumerable<string> items)
    {
        _items = items?.ToArray() ?? throw new ArgumentNullException(nameof(items));
    }

    private readonly string[] _items;

    public string this[int index] => _items[index];
    public int Count => _items.Length;

    public IEnumerator<string> GetEnumerator() => ((IEnumerable<string>)_items).GetEnumerator();
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _items.GetEnumerator();
}

Requirements

  • .NET 6.0 or higher
  • C# 10.0 or higher

License

AutoCollection is licensed under the MIT License.

Contributing

Contributions are welcome! Feel free to submit issues or pull requests on the GitHub repository.

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
0.1.21 133 5/26/2025
0.0.19 74 5/25/2025
0.0.0.61 290 5/21/2025