Search code examples
c#c#-7.2ref-struct

Why ref structs cannot be used as type arguments?


C# 7.2 introduced ref structs. However, given a ref struct like this:

public ref struct Foo {
  public int Bar;
}

I cannot use it as a type argument:

int i = 0;
var x = Unsafe.As<int, Foo>(ref i); // <- Error CS0306 The type 'Foo' may not be used as a type argument.

I understand that ref structs can only exist on the stack, and not the heap. But what if the generic method that would use such ref structs is guaranteed to never put them on the heap, as in the example above that uses System.Runtime.CompilerServices.Unsafe package? Why can I not use them in those cases as type parameters?


Solution

  • As of C# 13 and .NET 9, Generic parameters may now have an allows ref struct constraint. The compiler will then enforce ref struct like restrictions on the use of the type parameter within the method.

    For example, there is the .NET 9 implementation of Dictionary.GetAlternateLookup():

    public AlternateLookup<TAlternateKey> GetAlternateLookup<TAlternateKey>()
            where TAlternateKey : notnull, allows ref struct
    {
        if (!AlternateLookup<TAlternateKey>.IsCompatibleKey(this))
        {
            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IncompatibleComparer);
        }
    
    
        return new AlternateLookup<TAlternateKey>(this);
    }  
    

    Source: https://github.com/dotnet/runtime/blob/0cecd7d4d1f8a22c186a6145c6e60b393bf9518d/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs#L649-L658

    This, in turn, allows a ref struct type like ReadOnlySpan<char> to be used as an indexer, which was not previously possible:

    public readonly struct AlternateLookup<TAlternateKey> where TAlternateKey : notnull, allows ref struct
    {
        // ...
    
        /// <summary>Gets or sets the value associated with the specified alternate key.</summary>
        /// <param name="key">The alternate key of the value to get or set.</param>
        /// <value>
        /// The value associated with the specified alternate key. If the specified alternate key is not found, a get operation throws
        /// a <see cref="KeyNotFoundException"/>, and a set operation creates a new element with the specified key.
        /// </value>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
        /// <exception cref="KeyNotFoundException">The property is retrieved and alternate key does not exist in the collection.</exception>
        public TValue this[TAlternateKey key]
        {
            get
            {
                ref TValue value = ref FindValue(key, out _);
                if (Unsafe.IsNullRef(ref value))
                {
                    ThrowHelper.ThrowKeyNotFoundException(GetAlternateComparer(Dictionary).Create(key));
                }
    
    
                return value;
            }
            set => GetValueRefOrAddDefault(key, out _) = value;
        }
    
        // ...
    }
    

    Source: https://github.com/dotnet/runtime/blob/0cecd7d4d1f8a22c186a6145c6e60b393bf9518d/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs#L690-L724