提问人:Francesca Frattini 提问时间:5/2/2020 最后编辑:madreflectionFrancesca Frattini 更新时间:5/2/2020 访问量:360
始终返回 null 的方法
Method that always return null
问:
我被一段从 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 + ">";
}
}
答:
0赞
Francesca Frattini
5/2/2020
#1
好的,解决了。 正如你所说,问题出在 Pair 类上。 我用 ValueTuple<> 类替换它,现在一切正常,仍然感谢。
评论
Pair<,>
GetHashCode
Equals
(int, int)
this.map.TryGetValue(new Pair<int, int>(x, y), out Square? square);
Pair
new
new Pair<int, int>(x, y)
将不匹配现有的字典键,除非您已覆盖并在类中。默认行为是使用引用比较来确定是否相等,并且任何内容都不会引用匹配其他内容。请出示班级代码,以获得实际答案的帮助。Equals
GetHashCode
Pair
new
Pair
Pair<X,Y>
GetHashCode
Equals
(int,int)
ValueTuple<int,int>
Tuple<int,int>
int