简体   繁体   English

如何在 c# 中创建 int 指针的动态数组?

[英]How to create a dynamic array of int pointers in c#?

I want something like std::vector<int*> v from C++, but in C#.我想要来自 C++ 的std::vector<int*> v之类的东西,但在 C# 中。

I tried List<int*> v , but the compiler said that我试过List<int*> v ,但编译器说

CS0306 C# The type may not be used as a type argument CS0306 C# 该类型不能用作类型参数

I want to put int variables and change it through the array something like that in C++我想放入int变量并通过类似 C++ 中的数组进行更改

std::vector<int*> v;
int a = 2;
v.push_back(&a);
v[0] = 4; // (a == 4) true

First off let me say very clearly: solving problems in C# using the same low-level pointer techniques as you would in C++ is almost always the wrong thing to do .首先让我说得很清楚:使用与 C++ 相同的低级指针技术解决 C# 中的问题几乎总是错误的做法 There is usually a higher-level, safer technique you can use.您通常可以使用更高级别、更安全的技术。 What you're doing here is called an "XY question" -- you have a wrong idea about how to solve a problem, and you're asking a question about the wrong idea, instead of asking how to solve the real problem;你在这里所做的被称为“XY 问题”——你对如何解决问题有一个错误的想法,你问的是关于错误想法的问题,而不是问如何解决真正的问题; please edit the question to say what the real problem is.请编辑问题以说出真正的问题是什么。

To answer your question that was asked: to make a "dynamic array" of pointers in C# you must make a List<IntPtr> which is a list of "integers that are the same size as a pointer".要回答您提出的问题:要在 C# 中创建一个指针的“动态数组”,您必须创建一个List<IntPtr> ,它是一个“与指针大小相同的整数”的列表。 Do not fall into the trap of believing that IntPtr means pointer to integer .不要陷入相信IntPtr表示指向 integer 的指针的陷阱 An IntPtr is an integer that is the same size as a pointer . IntPtr与指针大小相同的 integer

You can then convert your int* s to IntPtr and store them in the list, and then convert them back again when you're done, but again, this is almost always the wrong thing to do , and if you do so then you are responsible for understanding everything about the .NET memory management system and how it works, because you are turning off the safety system that protects memory integrity .然后,您可以将您的int* s 转换为IntPtr并将它们存储在列表中,然后在完成后再次将它们转换回来,但同样,这几乎总是错误的做法,如果您这样做,那么您就是负责了解有关 .NET memory 管理系统及其工作原理的一切,因为您正在关闭保护 memory 完整性的安全系统

Do you believe that you understand everything about how memory management works in C#?您相信您了解有关 memory 管理在 C# 中如何工作的一切吗? If not, then do not use pointer types in C# .如果不是,则不要在 C# 中使用指针类型 I have been developing code in C# since 2001 and I have never used pointer types for anything in production code.自 2001 年以来,我一直在 C# 中开发代码,并且我从未在生产代码中使用过指针类型。 You don't need them, and if you think you need them, you're probably doing something wrong.你不需要它们,如果你认为你需要它们,你可能做错了什么。

You can solve the lack of having a pointer to a value type by creating a wrapper to the value like that:您可以通过为值创建一个包装器来解决缺少指向值类型的指针的问题,如下所示:

static void Test()
{
  var v = new List<EmbeddedInt>();
  var a = new EmbeddedInt(2);
  v.Add(a);
  v[0].Value = 4;
  Console.WriteLine(a);  // Display 4
}

public class EmbeddedInt
{
  public int Value { get; set; }
  public override string ToString()
  {
    return Value.ToString();
  }
  public EmbeddedInt(int value)
  {
    Value = value;
  }
}

This is a simple example here.这是一个简单的例子。

To be able to compute on the value without using a + 2 instead of a.Value + 2 you may implement some methods like operators.为了能够在不使用a + 2而不是a.Value + 2的情况下计算值,您可以实现一些方法,如运算符。

The only thing you can't do is overloading the assignment operator...你唯一不能做的就是重载赋值运算符......

Take a look at this:看看这个:

Define assigment operator that change the value of some "this" in C# 定义改变 C# 中某些“this”值的赋值运算符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM