将引用类型的值传递给 Dictionary.Add 方法
所以我有一个相当大的二维锯齿状数组,我想将其分成更小的数组。之后,我想将这些较小的二维锯齿状数组存储在字典中。
为此,我使用字典上的 .Add 方法。所以现在我添加了较小的数组,但似乎我只将存储较小数组的变量的引用添加到 Dictionary.Add 方法,或者至少这是我在 4 小时的主演后得出的结论在我的屏幕上,调试和 3 次精神崩溃。
因此,如果我为较小的数组更改变量(显然我这样做是因为我想将其分成多个文件片段),那么我最终也会更改存储在字典中的值,并且最终会得到一个存储始终相同的字典价值。
我也没有发现该错误,因为在更改代码之前,我为较小的数组运行了变量,抛出了另一个函数,或者至少我认为我就是这样做的。但由于某种原因我无法复制它。那么解决这个问题并将值而不是引用类型传递给我的 Dictionary.Add 方法的最佳方法是什么?我发现了 ref 关键字,但没有找到对应部分。
我的代码:
(currentChunkArray是我的较小数组的数组) (ChunkData.FalloffMaps 是我的字典)
float[][] currentChunkArray = new float[ChunkData.verticies + 2 * border][];
for (int i = 0; i < ChunkData.verticies + 2 * border; i++)
{
currentChunkArray[i] = new float[ChunkData.verticies + 2 * border];
}
for (int y = 0; y < ChunkData.islandSize; y++)
{
for (int x = 0; x < ChunkData.islandSize; x++)
{
for (int i = 0 ; i < ChunkData.verticies + 2 * border; i++)
{
float[] sourceArray = borderedIslandMap[i + y * ChunkData.verticies - y];
int sourceIndex = x * ChunkData.verticies - x;
float[] destinationArray = currentChunkArray[i];
int destinationIndex = 0;
int length = ChunkData.verticies + 2 * border;
Array.Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length);
}
ChunkData.FalloffMaps.Add(new Vector2(y, x), currentChunkArray);
So I have a quite large 2D jagged Array that I want to spilt up into smaller ones. After that I want to store those smaller 2D jagged Arrays in a Dictionary.
To do that I use the .Add method on the Dictionary. So now I add my smaller Arrays but it seams that I only add the reference of my variable that stores my smaller Arrays to the Dictionary.Add
method, or at least that is my conclusion after 4 hours of starring at my screen, Debugging and 3 mental breakdowns.
So if I change my variable for my smaller Arrays, which I obviously do because I want to spilt it up into multifile pieces I also end up changing the value that I store in my Dictionary and I end up with a Dictionary that stores always the same value.
I also didn't find that bug because before I changed my code, I ran my variable for my smaller Arrays threw another function or at least that is what I think I did. But I cant replicate that for some reason. So what is the best way to go about this and pass a value instead of a reference type to my Dictionary.Add method? I found out about the ref keyword, but not about a counter part.
My Code:
(currentChunkArray is my Array for the smaller Arrays)
(ChunkData.FalloffMaps is my Dictionary)
float[][] currentChunkArray = new float[ChunkData.verticies + 2 * border][];
for (int i = 0; i < ChunkData.verticies + 2 * border; i++)
{
currentChunkArray[i] = new float[ChunkData.verticies + 2 * border];
}
for (int y = 0; y < ChunkData.islandSize; y++)
{
for (int x = 0; x < ChunkData.islandSize; x++)
{
for (int i = 0 ; i < ChunkData.verticies + 2 * border; i++)
{
float[] sourceArray = borderedIslandMap[i + y * ChunkData.verticies - y];
int sourceIndex = x * ChunkData.verticies - x;
float[] destinationArray = currentChunkArray[i];
int destinationIndex = 0;
int length = ChunkData.verticies + 2 * border;
Array.Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length);
}
ChunkData.FalloffMaps.Add(new Vector2(y, x), currentChunkArray);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论