Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 549
TGY Avatar asked Feb 20 '19 15:02

TGY


People also ask

What is unmanaged type?

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.

What is difference between managed and unmanaged code?

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.

What is difference between managed and unmanaged resources?

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.

What do you mean by managed code and unmanaged code?

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.


1 Answers

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.

like image 130
Vlad Avatar answered Oct 04 '22 17:10

Vlad