C# foreaching 通过 list<struct> 不会改变值

C# foreaching through list<struct> won't change values

我有一个保存条件信息的结构。

private struct hintStructure
{
    public string id;
    public float  value;
    public bool   alreadyWarned;
}
private List<hintStructure> hints;

每当我的程序更改一个值时,都会发送一个事件并检查条件列表是否该元素满足条件。

public void EventListener (string id)
{
    CheckHints(id); //id of the updated element
}

private void CheckHints(string _id) 
{
    foreach (hintStructure _h in hints)
        if (_h.id == _id) CheckValue(_h);
}

private void CheckValue(hintStructure _h)
{
    float _f = GetValue(_h.id);

    if (_f < _h.value)
    {
        ActivateHint(_h);
        _h.alreadyWarned = true;
    }
    else 
        _h.alreadyWarned = false;
}

private void ActivateHint(hintStructure _h)
{
    if(_h.alreadyWarned == false)
        ShowPopup();
}

ShowPopup() 只应在尚未针对该特定元素显示时调用(由 bool alreadyWarned 表示)。问题是:它总是显示。似乎调用了 _h.alreadyWarned = true; 行,但未存储该值(我检查了它是否被调用,确实如此)。 我认为 foreach 可能是问题所在(因为它在几年前就出问题了),但它也不适用于 for() 结构。

我最后的猜测是寻址问题,C++ 中的典型问题: CheckValue(h); vs. CheckValue(&h); 但如果我的猜测是正确的 - 我该如何解决这个问题?

您正在传递结构,而不是 class 实例。对结构的更改(未作为 ref 传递)正在更改结构的本地副本。您正在尝试将结构设为引用类型。

在函数内对结构所做的更改不会更改原始结构的值 - 结构在传递给函数之前被复制到堆栈。

问题是 C# 中的结构是值类型,因此当您调用 CheckValue(_h) 时,您正在创建 _h 的副本并且该副本已更新, 但列表中的原始内容保持不变。

classic 解决方案通过引用传递此实例 (ref),但您不能使用 foreach 变量来实现。

解决方案是将 hintStructure 更改为 class:

private class Hint
{
    public string id;
    public float  value;
    public bool   alreadyWarned;
}