Pointers to C # and how often is it used in the application?

For me, a pointer was one of the most complex concepts in C ++ programming languages. When I studied C ++, I spent a huge amount of time learning this. However, now I mainly work on projects that are fully written in languages ​​such as C #, VB.NET, etc. In fact, I have NOT been concerned with C ++ for almost 4 years. Despite the fact that C # has a pointer, but I do not have a situation where I have to use a pointer to C #. So my question is: what kind of performance can we get in C # with a pointer? What is the situation when using a pointer is necessary?

+4
source share
4 answers

Personally, I have never had to use pointers in .NET, but if you are dealing with absolute critical code, you should use pointers. If you look at the System.String class, you will see that many methods that handle string manipulation use pointers. In addition, when processing images, it is often useful to use pointers. Now you can definitely say whether these applications should be written in .NET in the first place (I think they should), but at least if you need to squeeze this extra bit of speed, you can.

+2
source

You already use a lot of pointers to C #, except that they are not like pointers. Every time you do something with an instance of a class, this is a pointer. You get almost all of the potential benefits without the hassle.

You can use pointers more explicitly in C #, which most people mean pointers to C #, but I think cases will be very rare. They can be useful for communicating with C libraries, etc., but other than that I don’t see much benefit to them.

+3
source

I use pointers to C # only in rare cases, which are mainly related to sending / receiving data, where you need to convert an array of bytes to a structure and vice versa. Although even then you do not need to deal with pointers directly.

In some cases, you can use pointers to improve performance, because with Marshaller you sometimes have to copy memory to access data, and with pointers you can access it directly (think Bitmap.Lock() ).

+1
source

Personally, I never had to use a pointer to C #. If I need such functionality, I write this code in C ++ / CLI and call it from C #. If I need to pass pointers from C # to C ++ / CLI or vice versa, I pass them as IntPtr and pass it to the type that I need in C ++ / CLI.

In my opinion, if you use pointers to C #, in 99% of cases you use the language incorrectly.

Edit: The good thing about C ++ / CLI is that you can mark separate classes for compilation only for the native language. I do a lot of image processing work, which should happen very quickly; it uses a lot of code based on pointers. Usually I have a C ++ / CLI managed object to directly call my own C ++ object, where my processing takes place. I turn on optimization for this native code and viola, I get a good performance boost.

Of course, this only matters if the performance you get by running your own optimized code can offset the overhead of managed unmanaged transitions. In my case, this always happens.

0
source

Source: https://habr.com/ru/post/1286291/


All Articles