tags:

views:

416

answers:

4

For me , the Pointer was one of the hardest concept in programming languages in C++. When I was learning C++, I spent tremendous amount of time learning it. However, Now I primarily work in projects that are entirely written in languages like C#, and VB.NET etc. As a matter fact, I have NOT touched C++ for almost 4 years. Even though, C# has pointer, but I have not encouter the situation where I must use pointer in C#. So my question is , what kinds of productivity can we obtain in C# by using pointer ? what are the situation where the uses of the pointer is must?

A: 

Personally, I've never needed to use a pointer in C#. If I need that kind of functionality, I write that 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 around as an IntPtr and cast it to the type I need in C++/CLI.

In my opinion - if you're using pointers in C#, in 99% of cases, you're using the language wrong.

Edit: The nice thing about C++/CLI is that you can mark individual classes for native-only compilation. I do a lot of image processing work which needs to happen very quickly; it uses a lot of pointer-based code. I generally have a managed C++/CLI object forward calls to a native C++ object where my processing takes place. I turn on optimizations for that native code and viola, I get a nice performance gain.

Granted, this only matters if the performance gain you get by executing native, optimized code can offset the overhead of managed to unmanaged transitions. In my case, it always does.

unforgiven3
+1  A: 

I use pointers in C# only in rare circumstances that mostly have to do with sending/receiving data, where you have to convert a byte array to a struct and vice-versa. Though even then, you don't have to deal with pointers directly typically.

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

OregonGhost
Good point - I forgot about byte arrays to/from structs in my answer.
unforgiven3
I just had a look at the conversion code in a recent app and there I don't even really use pointers, just the GCHandle stuff. But sometimes in those cases, you need the fixed thing when dealing with data.
OregonGhost
+3  A: 

You're already using lots of pointers in C#, except that they don't look like pointers. Every time you do something with an instance of a class, that's a pointer. You're getting almost all the potential benefit already, without the hassle.

It is possible to use pointers more explicitly in C#, which is what most people mean by C# pointers, but I would think the occasions would be very rare. They may be useful to link to C libraries and the like, but other than that I don't see much use for them.

David Thornley
I think Shiva asks about raw pointers, not about object references that are basically pointers in disguise :)
OregonGhost
Exactly, OregonGhost is right
Shiva
However, Shiva seemed to be wondering why, if they were so important in C++, they seem so unimportant in C#. Part of the answer is that most of what a good C++ program uses pointers for is exactly what C# and Java and Common Lisp, etc., abstract away as object references or the like. That's why C# and similar languages lose almost no functionality by not having pointer types, or at least commonly used pointer types.
David Thornley
+1  A: 

Personally, I've never had a need for using pointers in .NET, but if you're dealing with absolute performance critical code, you'd use pointers. If you look at the System.String class, you'll see that a lot of the methods that handle the string manipulation, use pointers. Also, when dealing with image processing, very often it's useful to use pointers. Now, one can definitely argue whether those sort of applications should be written in .NET in the first place (I think they should), but at least if you need to squeeze out that extra bit of speed, you can.

BFree