UnlimitedSharp.StructIterator 1.0.0

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

This package enables the use of yield return in methods that return struct types (zero-allocation iterators). To use it, you just need to declare a struct with a static method, that cointains yield statements, and whose return type is the struct itself. The struct must be non-partial, and it must contain no members other than the static iterator method. It must also implement the IEnumerator<T> interface, and no other interfaces. The static iterator method can have any name, but it must be non-async and non-generic (the containing struct can be generic).

public struct MyIterator : IEnumerator<int>
{
    public static MyIterator Create()
    {
        yield return 1;
        yield return 2;
        yield return 3;
    }
}

The compiler will then generate the following code, declaring a private field in the struct for the iterator state machine, and generating the IEnumerator<T> implementation members, delegating to that field. The internal state machine type will also be generated as a struct, so no allocations will happen:

public struct MyIterator : IEnumerator<int>
{
    private MyIterator.<Create>d__0 _stateMachine;

    public int Current => _stateMachine.Current;
    public void Dispose() => _stateMachine.Dispose();
    public bool MoveNext() => _stateMachine.MoveNext();
    public void Reset() => _stateMachine.Reset();
    object IEnumerator.Current => ((IEnumerator)_stateMachine).Current;

    public MyIterator GetEnumerator() => this;

    private struct <Create>d__0 : IEnumerator<int>
    {
        // state machine implementation
    }
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.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 71 3/4/2026