Categories

Dec 8, 2019 in Dot Net
Q: Unmanaged constructed types in C#

1 Answer

Dec 8, 2019

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 } };

Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

Madanswer
Oct 18, 2019 in C Sharp (C#)
Dec 8, 2019 in Dot Net
Dec 8, 2019 in Dot Net
...