什么是是非非托管类型和托管类型之间的区别? [英] What's the difference between a non-unmanaged type and a managed type?

查看:268
本文介绍了什么是是非非托管类型和托管类型之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写了下面的代码片段为实验目的,提出了悬停误差(见截图):

When I wrote the following snippet for experimenting purposes, it raised the hover-error (see screenshot):

无法声明指针非非托管式动态

Cannot declare pointer to non-unmanaged type 'dynamic'

该片段:

dynamic* pointerToDynamic = &fields;



虽然明确不允许的代码(你不能把一个托管类型的地址),它提出我的问题是:什么是的非非托管类型?它不同于托管类型?或只是Visual Studio的努力很有趣?

While the code is clearly not allowed (you cannot take the address of a managed type), it raised with me the question: what is a non-unmanaged type and how is it different to a managed type? Or is it just Visual Studio trying to be funny?

推荐答案

<击>有非托管和非托管的指针之间的差异。

一个管理的指针的是一个句柄,在托管堆中的对象,据我所知是在托管C ++才可用。它等价的C#的参考的一个对象。非托管指针,而另一方面,就相当于一个传统的C风格的指针,即一个存储单元的地址; C#提供了一元&安培; 操作符,固定关键字和不安全上下文的。

A managed pointer is a handle to an object on the managed heap, and AFAIK is available in managed C++ only. It is equivalent of C# reference to an object. Unmanaged pointer, on the other hand, is equivalent of a traditional C-style pointer, i.e. address of a memory location; C# provides unary & operator, fixed keyword and unsafe context for that.

您正试图获得一个指向一个管理域(动态实际上 System.Object的是伪装),而C#允许指针的非托管的对象而已,因此措辞:你的类型为非非托管

You are trying to get a pointer to a managed field (dynamic is actually System.Object is disguise), while C# allows pointers to unmanaged objects only, hence the wording: your type is non-unmanaged.

更多关于这里此一点。

A bit more on this here.

更新:,以使其更清晰,托管C ++支持经典的C风格的指针的和的引用。但是为了保持C ++术语一致,他们被称为的非托管的和的管理指针后,相应地。 C#也支持指针(明确地不安全上下文)和引用(隐时引用类型的对象是参与),但后者的不可以被称为管理指针,他们只是引用

Update: to make it more clear, managed C++ supports classic C-style pointers and references. But to keep C++ terminology consistent, they are called unmanaged and managed pointers, correspondingly. C# also supports pointers (explicitly in unsafe context) and references (implicitly whenever objects of reference types are involved), but the latter is not called "managed pointers", they are just references.

要总结一下:。在C ++中有非托管和托管指针,在C#中 - 非托管指针和引用

To sum up: in C++ there are unmanaged and managed pointers, in C# - unmanaged pointers and references.

希望这是有道理的吧。

这篇关于什么是是非非托管类型和托管类型之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆