C#中的指针以及它在应用程序中的使用频率?

时间:2022-09-02 11:03:48

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?

对我来说,Pointer是C ++中编程语言中最难的概念之一。当我学习C ++时,我花了很多时间学习它。但是,现在我主要在完全用C#,VB.NET等语言编写的项目中工作。事实上,我已经有近四年没有接触过C ++了。尽管如此,C#有指针,但我还没有考虑我必须在C#中使用指针的情况。所以我的问题是,通过使用指针,我们可以在C#中获得什么样的生产力?必须使用指针的情况是什么?

4 个解决方案

#1


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.

就个人而言,我从来没有需要在.NET中使用指针,但如果你正在处理绝对性能关键代码,你就会使用指针。如果你看一下System.String类,你会发现很多处理字符串操作的方法都使用指针。此外,在处理图像处理时,使用指针通常很有用。现在,人们可以肯定地说这些应用程序是否应该首先用.NET编写(我认为应该这样),但至少如果你需要挤出额外的速度,你可以。

#2


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.

你已经在C#中使用了很多指针,除了它们看起来不像指针。每次使用类的实例执行某些操作时,这都是指针。你已经获得了几乎所有潜在的好处,没有麻烦。

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.

可以在C#中更明确地使用指针,这是C#指针大多数人的意思,但我认为这种情况非常罕见。它们可能对链接到C库等很有用,但除此之外我没有看到它们有多大用处。

#3


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.

我只在极少数情况下使用C#中的指针,这些情况主要与发送/接收数据有关,你必须将字节数组转换为结构,反之亦然。即便如此,您也不必直接处理指针。

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

在某些情况下,您可以使用指针来提高性能,因为使用Marshaller时,有时您必须复制内存才能访问数据,而使用指针,您可以直接访问它(想想Bitmap.Lock())。

#4


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.

就个人而言,我从来不需要在C#中使用指针。如果我需要这种功能,我在C ++ / CLI中编写该代码,并从C#中调用它。如果我需要将指针从C#传递到C ++ / CLI,反之亦然,我将它们作为IntPtr传递给它并将其转换为我在C ++ / CLI中需要的类型。

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

在我看来 - 如果您在C#中使用指针,在99%的情况下,您使用的语言是错误的。

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.

编辑:关于C ++ / CLI的好处是你可以标记各个类的纯本机编译。我做了很多图像处理工作,需要很快发生;它使用了很多基于指针的代码。我通常有一个托管C ++ / CLI对象转发调用我的处理发生的本机C ++对象。我打开了原生代码和中提琴的优化,我获得了不错的性能提升。

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.

当然,只有通过执行本机优化代码获得的性能提升可以抵消托管到非托管转换的开销,这才有意义。就我而言,它总是如此。

#1


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.

就个人而言,我从来没有需要在.NET中使用指针,但如果你正在处理绝对性能关键代码,你就会使用指针。如果你看一下System.String类,你会发现很多处理字符串操作的方法都使用指针。此外,在处理图像处理时,使用指针通常很有用。现在,人们可以肯定地说这些应用程序是否应该首先用.NET编写(我认为应该这样),但至少如果你需要挤出额外的速度,你可以。

#2


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.

你已经在C#中使用了很多指针,除了它们看起来不像指针。每次使用类的实例执行某些操作时,这都是指针。你已经获得了几乎所有潜在的好处,没有麻烦。

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.

可以在C#中更明确地使用指针,这是C#指针大多数人的意思,但我认为这种情况非常罕见。它们可能对链接到C库等很有用,但除此之外我没有看到它们有多大用处。

#3


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.

我只在极少数情况下使用C#中的指针,这些情况主要与发送/接收数据有关,你必须将字节数组转换为结构,反之亦然。即便如此,您也不必直接处理指针。

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

在某些情况下,您可以使用指针来提高性能,因为使用Marshaller时,有时您必须复制内存才能访问数据,而使用指针,您可以直接访问它(想想Bitmap.Lock())。

#4


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.

就个人而言,我从来不需要在C#中使用指针。如果我需要这种功能,我在C ++ / CLI中编写该代码,并从C#中调用它。如果我需要将指针从C#传递到C ++ / CLI,反之亦然,我将它们作为IntPtr传递给它并将其转换为我在C ++ / CLI中需要的类型。

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

在我看来 - 如果您在C#中使用指针,在99%的情况下,您使用的语言是错误的。

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.

编辑:关于C ++ / CLI的好处是你可以标记各个类的纯本机编译。我做了很多图像处理工作,需要很快发生;它使用了很多基于指针的代码。我通常有一个托管C ++ / CLI对象转发调用我的处理发生的本机C ++对象。我打开了原生代码和中提琴的优化,我获得了不错的性能提升。

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.

当然,只有通过执行本机优化代码获得的性能提升可以抵消托管到非托管转换的开销,这才有意义。就我而言,它总是如此。