Search code examples
c#sizeofunmanagedmanaged

Managed vs. unmanaged types


I was reading an article about how to use the sizeof operator in C#.

They say: "Used to obtain the size in bytes for an unmanaged type."

I know the difference between managed and unmanaged code. But my understanding is that all code I write in C# (including all predefined and user-defined types) is managed by the CLR. So what do they mean by "unmanaged types"?


Solution

  • The term "unmanaged type" is a little bit misleading: is not a type which is defined in unmanaged code. It's rather a type which doesn't contain references managed by the garbage collector.

    In C# 7.3 there is even a generic constraint unmanaged:

    [...] must not be a reference type and must not contain any reference type members at any level of nesting.


    If you have experience with WinAPI: the originally proposed name for unmanaged types was blittable.