Managed vs. unmanaged types
Asked Answered
S

1

16

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"?

Spermato answered 20/2, 2019 at 15:49 Comment(4)
You can of course use types from a library written in C/C++ in your .NET-program. Those are called "unmanaged types".Terrill
Possible duplicate of #3564370Pedroza
There is actually definition on the page you are referring to. In a simple words: strict that contains no references itself and in all the containing types, all the way down. It's called unmanaged because it doesn't contain or refer to any data which is managed by the garbage collector.Concelebrate
@Amy: Not exactly a duplicate: a struct completely defined in managed code can be an unmanaged struct according to the definition at OP's link.Concelebrate
C
22

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.

Concelebrate answered 20/2, 2019 at 16:13 Comment(2)
Here is the list of all unmanaged types: learn.microsoft.com/en-us/dotnet/csharp/language-reference/…Lattimer
@gonzobrains: Well, it's not exactly a list, it's a self-recursive definition, but it contains a list of primitive unmanaged types.Concelebrate

© 2022 - 2024 — McMap. All rights reserved.