What is a situation while coding in C# where using pointers is a good or necessary option? I'm talking about unsafe pointers.
|
|
From the developer of C# himself: The use of pointers is rarely required in C#, but there are some situations that require them. As examples, using an unsafe context to allow pointers is warranted by the following cases:
The use of unsafe context in other situations is discouraged. Specifically, an unsafe context should not be used to attempt to write C code in C#. Caution: "Code written using an unsafe context cannot be verified to be safe, so it will be executed only when the code is fully trusted. In other words, unsafe code cannot be executed in an untrusted environment. For example, you cannot run unsafe code directly from the Internet." You may go through this for reference |
|||
|
|
|
I once had to use pointers (in the unsafe context) in a C# based windows application that would act as an interface to a headset. This application is a user interface that would allow agents (at a call center) to control their headphone settings. This application acts as an alternate to the control panel given by the headset manufacturer. Thus, their ability to control the headsets were limited when compared to the options available. I had to use pointers because I had to use the API (a Visual C++ dll) provided by the headset manufacturer using P/Invoke. |
|||
|
|
|
yes, there are real uses, when performance is critical and the operations are low-level for example, i've only needed to use pointers in C# once, for image comparison. Using GetPixel on a pair of 1024x1024x32 images took 2 minutes to do the comparison (Exact match). Pinning the image memory and using pointers took less than 1 second (on the same machine of course). |
|||||||||||||||
|
|
You have to remember that the designers at Microsoft are smart people and everything they add to C# has at least 1 use case. The FParsec project uses unsafe code to bring out every last drop of performance that C# is capable of. Take notice of the usage of
|
|||||||||
|