© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
7 replies
Eyrim

❔ Unable to box custom value type

Hiya, I'm writing a library to interact with MIDI files. I'm laying down some framework stuff (seven bit numbers, Variable Length Quantities [VLQ] etc.), the VLQ's and seven bit numbers are implemented as structs currently.

using System;
using MidiLibrary.Util;

namespace MidiLibrary.Structures
{
    public readonly struct VariableLengthQuantity
    //TODO: Override ToString
    {
        private readonly bool[][] Number { get; init; }

        private VariableLengthQuantity(long number)
        {
            bool[] binaryRep = BinaryTools.LongToBinary(number);
            bool[][] sevenBitNumbers = BinaryTools.BinaryToSevenBitNumbers(binaryRep);
            // Reverse the array
            Array.Reverse(sevenBitNumbers, 0, sevenBitNumbers.Length);
            
            for (int i = 0; i < sevenBitNumbers.Length; i++)
            {
                sevenBitNumbers[i] = BinaryTools.PadBinary(sevenBitNumbers[i], 8);
                // everything that isn't next, has a 1 (true) as MSB
                sevenBitNumbers[i][^1] = true;
            }

            sevenBitNumbers[0][^1] = false;

            this.Number = sevenBitNumbers;
        }
        
        public static explicit operator VariableLengthQuantity(long number) => new VariableLengthQuantity(number);

        private static byte[] FromLong(long number)
        {
            throw new NotImplementedException();
        }

        private static long ToLong(byte[] array)
        {
            throw new NotImplementedException();
        }
    }
}
using System;
using MidiLibrary.Util;

namespace MidiLibrary.Structures
{
    public readonly struct VariableLengthQuantity
    //TODO: Override ToString
    {
        private readonly bool[][] Number { get; init; }

        private VariableLengthQuantity(long number)
        {
            bool[] binaryRep = BinaryTools.LongToBinary(number);
            bool[][] sevenBitNumbers = BinaryTools.BinaryToSevenBitNumbers(binaryRep);
            // Reverse the array
            Array.Reverse(sevenBitNumbers, 0, sevenBitNumbers.Length);
            
            for (int i = 0; i < sevenBitNumbers.Length; i++)
            {
                sevenBitNumbers[i] = BinaryTools.PadBinary(sevenBitNumbers[i], 8);
                // everything that isn't next, has a 1 (true) as MSB
                sevenBitNumbers[i][^1] = true;
            }

            sevenBitNumbers[0][^1] = false;

            this.Number = sevenBitNumbers;
        }
        
        public static explicit operator VariableLengthQuantity(long number) => new VariableLengthQuantity(number);

        private static byte[] FromLong(long number)
        {
            throw new NotImplementedException();
        }

        private static long ToLong(byte[] array)
        {
            throw new NotImplementedException();
        }
    }
}


This is the boxing allocation I'm trying to use:
VariableLengthQuantity a = (VariableLengthQuantity) 137;
object aa = a;
VariableLengthQuantity a = (VariableLengthQuantity) 137;
object aa = a;


Full disclosure, I'm pretty new to using structs. I'd normally just throw it into a class but figure I should probably branch out to something better.

Boxing allocation: conversion from 'VariableLengthQuantity' to 'object' requires boxing of value type
Boxing allocation: conversion from 'VariableLengthQuantity' to 'object' requires boxing of value type
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

cannot convert from 'TypeA' to 'TypeB' with generics
C#CC# / help
2y ago
❔ Maximum value in a text box
C#CC# / help
3y ago
Unable to get the FieldInfo's value
C#CC# / help
2y ago
✅ Unable to create migration - 'Unable to resolve service for type 'DbContextOptions'
C#CC# / help
2y ago