0 votes
in Dot Net by
Unmanaged constructed types in C#

1 Answer

0 votes
by

In C# 7.3 and earlier, a constructed type (a type that includes at least one type argument) can't be an unmanaged type. Starting with C# 8.0, a constructed value type is unmanaged if it contains fields of unmanaged types only.

For example, given the following definition of the generic Coords<T> type:

public struct Coords<T>
{
    public T X;
    public T Y;
}

the Coords<int> type is an unmanaged type in C# 8.0 and later. Like for any unmanaged type, you can create a pointer to a variable of this type or allocate a block of memory on the stack for instances of this type:

Span<Coords<int>> coordinates = stackalloc[] { new Coords<int> { X = 0, Y = 0 }, new Coords<int> { X = 0, Y = 3 }, new Coords<int> { X = 4, Y = 0 } };

...