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"?
A type is an unmanaged type if it's any of the following types: sbyte , byte , short , ushort , int , uint , long , ulong , char , float , double , decimal , or bool. Any enum type. Any pointer type.
Difference between managed and unmanaged code? Managed code is the one that is executed by the CLR of the . NET framework while unmanaged or unsafe code is executed by the operating system. The managed code provides security to the code while undamaged code creates security threats.
Managed resources are those that are pure . NET code and managed by the runtime and are under its direct control. Unmanaged resources are those that are not. File handles, pinned memory, COM objects, database connections etc.
Managed code is the code which is managed by the CLR(Common Language Runtime) in .NET Framework. Whereas the Unmanaged code is the code which is directly executed by the operating system.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With