问题描述
VB.NET 中与指针最相似的东西是什么,意思是像 C 指针一样?
What is the most similar thing in VB.NET to a pointer, meaning like C pointers?
我在一个类中有一个 TreeView
.我需要公开一些可以被外部对象修改的特定节点(或叶子).
I have a TreeView
within a class. I need to expose some specific nodes (or leaves) that can be modified by external objects.
推荐答案
Nathan W 已经建议了 IntPtr 结构,它可以表示一个指针或句柄,然而,虽然这个结构是 .NET 框架的一部分,但 .NET 确实没有每个指针-说,当然不喜欢 C 指针.
Nathan W has already suggested the IntPtr structure which can represent a pointer or handle, however, whilst this structure is part and parcel of the .NET framework, .NET really doesn't have pointers per-say, and certainly not like C pointers.
这主要是因为 .NET Framework 是一个托管"平台,内存由 CLR 管理、分配、分配和释放,开发人员无需担心(即没有 malloc 命令!)这主要是由于这种内存管理,您实际上无法访问直接内存地址.
This is primarily because the .NET Framework is a "managed" platform and memory is managed, assigned, allocated and deallocated by the CLR without you, the developer, having to worry about it (i.e. no malloc commands!) It's mostly because of this memory management that you don't really have access to direct memory addresses.
.NET Framework 中最接近可以被认为是指针"(但实际上不是)的是 委托.您可以将 delegate 视为函数指针",然而,它并不是最严格意义上的指针.Delegates 为调用函数添加类型安全,允许代码调用"一个委托实例以确保它使用正确的参数调用正确的方法.这与传统"指针不同,因为它们不是类型安全的,只是引用内存地址.
The closest thing within the .NET Framework that can be thought of as a "pointer" (but really isn't one) is the delegate. You can think of a delegate as a "function pointer", however, it's not really a pointer in the strictest sense. Delegates add type-safety to calling functions, allowing code that "invokes" a delegate instance to ensure that it is calling the correct method with the correct parameters. This is unlike "traditional" pointers as they are not type-safe, and merely reference a memory address.
代理在 .NET Framework 中无处不在,无论何时您使用 事件,或响应事件,您正在使用委托.
Delegates are everywhere in the .NET Framework, and whenever you use an event, or respond to an event, you're using delegates.
如果您想使用 C# 而不是 VB.NET,您可以编写标记为不安全".这允许不安全块中的代码在 CLR 的保护之外运行.这反过来又允许使用真实的"指针,不过就像 C 一样,它们仍然有一些限制(例如指向的内存地址可以是什么).
If you want to use C# rather than VB.NET, you can write code that is marked as "unsafe". This allows code within the unsafe block to run outside of the protection of the CLR. This, in turn, allows usage of "real" pointers, just like C, however, they still do have some limitations (such as what can be at the memory address that is pointed to).
这篇关于VB.NET 中 C 指针的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!