始终返回 null 的方法

Method that always return null

提问人:Francesca Frattini 提问时间:5/2/2020 最后编辑:madreflectionFrancesca Frattini 更新时间:5/2/2020 访问量:360

问:

我被一段从 Java 翻译成 C# 的代码卡住了。

基本上,我有一个 Map(Dictionary),其中的键由 Pair 组成,Values 由我创建的类 (Square) 表示;在这个类中,只有一个字段,它是 Optional(是的,我在 C# 中创建了 Optional 类)。

一开始,我用成对填充这个字典,以形成一个类似的网格和空的可选,如下面的代码所示。

class World
{
    private Dictionary<Pair<int, int>, Square> map = 
        new Dictionary<Pair<int, int>, Square>();

    public World(int width, int height)
    {
        this.size = new Pair<int, int>(width, height);
        for (int w = 0; w < this.size.GetX(); w++)
        {
            for (int h = 0; h < this.size.GetY(); h++)
                this.map.Add(new Pair<int, int>(w, h), 
                    new Square(Optional<Entity>.Empty()));
        }
    }
}

这是 Square 类

class Square
{
    private Optional<Entity> entity;

    public Square (Optional<Entity> entity)
    {
        this.entity = entity;
    }

    public Optional<Entity> GetEntity() 
    {
        return this.entity;
    }

    public void SetEntity(Optional<Entity> entity)
    {
        this.entity = entity;
    }
}

问题来了,当我尝试从字典中获取现有值时,下面的函数总是返回 null,它会抛出 System.NullReferenceException:对象引用未设置为对象的实例。 在此代码中,我删除了所有控件,但我知道我尝试获取已插入的值;另外,我尝试运行 Dictionary.ContainsValue 并返回 false!但我确实对词典进行了编辑。

public Square? GetSquare(int x, int y)
{
    if (y < this.size.GetY() && y >= 0 && < x this.size.GetX() && x >= 0)
    {
        this.map.TryGetValue(new Pair<int, int>(x, y), out Square? square);
        return square;
    }

    throw new InvalidOperationException("no square in this position!");
}

我也在这里留下了 Optional 类的代码,但我几乎 100% 确定这不是问题

public class Optional<T>
{
    private T value;
    public bool IsPresent { get; set; } = false;

    private Optional() { }

    public static Optional<T> Empty()
    {
        return new Optional<T>();
    }

    public static Optional<T> Of(T value)
    {
        Optional<T> obj = new Optional<T>();
        obj.Set(value);
        return obj;
    }

    private void Set(T value)
    {
        this.value = value;
        this.IsPresent = true;
    }

    public T Get()
    {
        return value;
    }
}

这是 Pair 类

    public class Pair<X, Y>
    {
        private X first;
        private Y second;

        public Pair(X first, Y second)
        {
            this.first = first;
            this.second = second;
        }
        public X GetX()
        {
            return this.first;
        }

        public Y GetY()
        {
            return this.second;
        }

        public override string ToString()
        {
            return "<" + first + "," + second + ">";
        }
    }
java c# null nullreferenceexception

评论

1赞 madreflection 5/2/2020
什么?它可能有一个坏的或实现。最好改用键的值元组(即)。Pair<,>GetHashCodeEquals(int, int)
0赞 Xerillio 5/2/2020
除非是结构类型,否则你永远不会得到任何结果。我很确定这在 Java 中也行不通。看看那一行中的逻辑:“给我与此对象关联的值”——由于此对象是新的,因此不会与它关联的任何值this.map.TryGetValue(new Pair<int, int>(x, y), out Square? square);Pairnew
1赞 Rufus L 5/2/2020
new Pair<int, int>(x, y)匹配现有的字典键,除非您已覆盖并在类中。默认行为是使用引用比较来确定是否相等,并且任何内容都不会引用匹配其他内容。请出示班级代码,以获得实际答案的帮助。EqualsGetHashCodePairnewPair
0赞 madreflection 5/2/2020
对于您的用例来说,您的是不够的。在没有实现和的情况下,Rufus L 所描述的正是正在发生的事情。同样,我建议使用值元组(即 ,不是 )作为您的键。它正确地实现了这些方法(当然,要求元组中的类型也正确地实现了它)。Pair<X,Y>GetHashCodeEquals(int,int)ValueTuple<int,int>Tuple<int,int>int

答:

0赞 Francesca Frattini 5/2/2020 #1

好的,解决了。 正如你所说,问题出在 Pair 类上。 我用 ValueTuple<> 类替换它,现在一切正常,仍然感谢。