Vb.net Pointers

前端 未结 5 2012
执念已碎
执念已碎 2021-01-14 16:20

What is the most similar thing in vb.net to a pointer, meaning like C poinetrs?

I have a TreeView within a class. I need to expose some specific nodes (or leaves) th

5条回答
  •  一整个雨季
    2021-01-14 17:09

    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.

    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.

    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.

    Delegates are everywhere in the .NET Framework, and whenever you use an event, or respond to an event, you're using delegates.

    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).

提交回复
热议问题