Table of Contents

Connect a field to its bytes

Intermediate · C#. Debug ranges use an exclusive end position: [1,3) means offsets 1 and 2. The debug result includes the root wrapper.

Run this example

Prerequisites: the repository's .NET 10 SDK and a checkout of this source. Run from the repository root:

dotnet run --project docs/examples/CStructSharp.Docs.Examples.csproj -c Release -- inspect-ranges

The runner checks uint16 occupies offsets 1 and 2; ResolveAddress returns 1. Success includes PASS inspect-ranges.

Try the related browser lesson.

Complete program

The layout, options, input bytes, helper methods, and required types are all included. To adapt it outside the repository, create a .NET 10 console project, add CStructSharp, and replace Program.cs with this complete file. These examples follow the source version; use a matching package when testing a release.

Download the complete C# source.

// Generated from executable documentation examples. Edit the source region, then regenerate.
using System;
using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Buffers;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using CStructSharp;
using CStructSharp.Codecs;
using CStructSharp.Diagnostics;
using CStructSharp.Introspection;
using CStructSharp.Values;

internal static partial class Program
{
    public static void Main()
    {
        InspectRanges();
        Console.WriteLine("PASS inspect-ranges");
    }

    private static void InspectRanges()
    {
        var layout = new CStruct("struct sample { uint8 tag; uint16 value; };");
        using var stream = new MemoryStream([0xA1, 0x34, 0x12]);
        (StructValue result, IReadOnlyList<DebugData> ranges) = layout.ParseWithDebug(stream, "sample");
        Equal((byte)0xA1, result.Get<byte>("tag"));
        True(ranges.Any(item => item.Start == 1 && item.End == 3), "Value range was not reported.");

        stream.Position = 0;
        Equal(1L, layout.ResolveAddress(stream, "sample.value"));
        Equal(0L, stream.Position);
    }

    private static void Equal<T>(T expected, T actual)
    {
        if (!EqualityComparer<T>.Default.Equals(expected, actual))
        {
            throw new InvalidOperationException($"Expected '{expected}', received '{actual}'.");
        }
    }

    private static void True(bool condition, string message)
    {
        if (!condition)
        {
            throw new InvalidOperationException(message);
        }
    }
}

Try it and diagnose mistakes

Change the tag byte only.

Answer: The value range remains [1,3). Changing a value does not change these fixed field positions. The program contains assertions for its original inputs. When changing an input intentionally, update the expected assertion too; an unchanged assertion is not evidence that the new value is wrong.

Continue with the related guide or choose another recipe.