![]() Notice that this is not deferencing the pointer but showing you the pointer's contents. Of course this assumes that the current implementation of int is large enough to contain a pointer, i.e. MessageBox.Show(((int)pMyInt).ToString()) So, to display the contents of the pointer you would use something like: What you can do, however, is to use a cast to convert a pointer to a more usual data type. For example, you can’t use a ToString() method call to display the value of a pointer. The first important thing to note is that a pointer does not inherit from an object and so there are no methods associated with it and no boxing and unboxing. pMyInt and stores the address of the integer MyInt in the pointer. The * is the dereferencing or indirection operator and is generally used in conjunction with the address of operator &, which as its name suggests, returns the address of a variable.Ĭreates a pointer to an integer, i.e. To declare a pointer type C# uses the C++ like syntax: to use pointer arithmetic, you need to cast a void pointer to a pointer to a given type. a pointer to an unknown type but to be of any use in, i.e. You can also have a pointer of type void, i.e. This is generally enough to allow pointers to be used to work with legacy implementations of data structures or to work with binary data structures that have been received over the network or from a file. However you can have a pointer to a struct that contains simple value types and you can create pointers to arrays of simple value types. This is restrictive and basically amounts to not allowing pointers to anything created on the heap or subject to dynamic memory management. So you can’t have a pointer to an object, or to a delegate or to a reference. int, float, char, to an enum, to another pointer or to a struct that doesn’t contain other managed types. Then any use of pointers has to be enclosed in an usafe block to mark it out even more clearly.Įven more restrictive is the fact that you cannot create a pointer to anything you care to point at, only to a restricted subset of types that have a simpler way of using memory so making pointer use slightly less tricky.Įssentially you can only create a pointer to any simple value type, e.g. First the entire project has to be marked as unsafe by using the Project Properties to set the "Build, Allow Unsafe Code" flag. ![]() Pointers are so dangerous that they have to be quarantined within your code. In other words, a reference that you can do arithmetic with, is a pointer. Pointers in C#' are a generalisation of the reference type to include pointer arithmetic.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |