【问题标题】:How can I create a list or array of references in c#?如何在 C# 中创建引用列表或数组?
【发布时间】:2014-07-12 15:58:56
【问题描述】:

如何创建一个列表或数组,其中每个元素都使用 ref 关键字,以便在值更改时列表仍指向原始引用指向的内存中的新位置?

编辑:我正在尝试创建许多对象,其唯一目的是保存对不同类别的其他对象的引用。从我的静态构造函数中查看这段代码:

normal = new Type("Normal", new Type[]{ghost}, new Type[]{rock, steel}, new Type[]{});
fighting = new Type ("Fighting", new Type[]{ghost}, new Type[]{flying, poison, bug, psychic, fairy}, new Type[]{normal, rock, steel, ice, dark});
flying = new Type ("Flying", new Type[]{}, new Type[]{rock, steel, electric}, new Type[]{bug, grass, fairy});
ground = new Type ("Ground", new Type[]{flying}, new Type[]{bug, grass}, new Type[]{poison, rock, steel, fire, electric});
rock = new Type ("Rock", new Type[]{}, new Type[]{fighting, ground, steel}, new Type[]{flying, bug, fire, ice});
bug = new Type ("Bug", new Type[]{}, new Type[]{fighting, flying, poison, ghost, steel, fire, fairy}, new Type[]{grass, psychic, dark});
ghost = new Type ("Ghost", new Type[]{normal}, new Type[]{dark}, new Type[]{ghost, psychic});
steel = new Type ("Steel", new Type[]{}, new Type[]{steel, fire, water, electric}, new Type[]{rock, ice, fairy});
fire = new Type ("Fire", new Type[]{}, new Type[]{ref Rock, ref Fire, ref Water, ref Dragon}, new Type[]{Bug, Ice, Steel, Grass});//TODO: move Ice back to the end
water = new Type ("Water", new Type[]{}, new Type[]{water, grass, dragon}, new Type[]{ground, rock, fire});
grass = new Type ("Grass", new Type[]{}, new Type[]{flying, poison, bug, steel, fire, grass, dragon}, new Type[]{ground, rock, water});
electric = new Type ("Electric", new Type[]{ground}, new Type[]{grass, electric, dragon}, new Type[]{flying, water});
psychic = new Type ("Psychic", new Type[]{dark}, new Type[]{steel, psychic}, new Type[]{fighting, poison});
ice = new Type ("Ice", new Type[]{}, new Type[]{steel, fire, water, ice}, new Type[]{flying, ground, grass, dragon});
dragon = new Type ("Dragon", new Type[]{fairy}, new Type[]{steel}, new Type[]{dragon});
dark = new Type ("Dark", new Type[]{}, new Type[]{fighting, dark, fairy}, new Type[]{ghost, psychic});
fairy = new Type ("Fairy", new Type[]{}, new Type[]{poison, steel, fire}, new Type[]{fighting, ice, dragon});

问题在于数组中的值仅适用于在当前创建对象之前创建的对象。所以,我想通过 ref 传递每个数组的成员,以便它们在分配后指向内存中的正确位置。

【问题讨论】:

  • 在 C# 中,数组已经被引用操作了。

标签: c# reference pass-by-reference ref


【解决方案1】:

你应该把它变成一个两步的过程,而不是一个,这样你才能得到对对象的实际引用。

你的Type 应该有这样的界面:

public class Type
{
     public Type(string key)
     {
        // store key         
     }

     public void Init(Type[] first, Type[] second, Type[] third)
     {
         // store or handle the parameters
     }
}

现在你的静态构造函数可以这样了

normal = new Type("Normal");
rock = new Type ("Rock");
ghost = new Type ("Ghost");
steel = new Type ("Steel");
// other stuff here

normal.Init(new Type[]{ghost}, new Type[]{rock, steel}, new Type[]{});
// other inits here

【讨论】:

  • 啊,是的,我曾想过做类似的事情,但如果我可以只使用原始参考,这似乎是多余的。我刚刚完成了这个,非常感谢!
【解决方案2】:

您是否在处理值类型并希望它们是引用类型

如果是这样,你必须做一些不愉快的事情,比如将你的值类型包装在一个类中,或者做一些 boxing 并将其作为引用类型传递。

【讨论】:

  • 自从您回复后,我添加了一个编辑,您能看一下吗?我认为这将有助于使问题更清晰。
  • 抱歉,问题是“如何在 C# 中创建 dangling pointers”?你确定你在做什么?
  • 好吧,在那个代码块之前,所有这些变量都被创建但没有被赋予一个值。我想创建一个包含实际变量引用的列表,因此一旦定义了以下变量,在它们之前定义的变量将指向原始变量现在指向的适当对象,而不是像现在那样指向 null 因为变量在按值传递时为空。我想要的和使用 ref 关键字完全一样,我只是想存储它们的列表。
  • @Dustuu 你不能在 C# 中做到这一点。 (也许可以用不安全的指针做一些黑客攻击,尽管这可能不是最好的方法)
  • @MAV 谢谢,很高兴知道。考虑到 ref 关键字的逻辑,我只是​​认为这是有道理的。有点遗憾,它不像我想的那样工作,但我找到了一个解决方案,所以一切都很好。
猜你喜欢
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2017-06-17
相关资源
最近更新 更多