?
---发布后更新为在第一个代码上发现的许多错误---
我在对象上使用布尔值时遇到了一些问题。这是我的 Chunk 类,其中包含一个 Zone 结构
:using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
new Chunk();
}
}
public class Chunk {
List <Zone> zones;
public Chunk() {
// Create 3 elements on the List
this.zones = new List<Zone>();
this.zones.Add(new Zone(1));
this.zones.Add(new Zone(2));
this.zones.Add(new Zone(42));
// Add coord to 2th zones
this.zones[0].AddCoord(1);
Console.WriteLine("Count : " + this.zones[0].coords.Count); // -> Count: 1
// Now with bool - NOT WORKING
this.zones[0].SetBool();
Console.WriteLine("Bool: " + this.zones[0].isOnChunkBorder ); // -> Bool: False
// Now with bool - WORKING
this.zones[0] = this.zones[0].SetBoolAndReturnThis();
Console.WriteLine("Bool: " + this.zones[0].isOnChunkBorder ); // -> Bool: True
}
public struct Zone {
public bool isOnChunkBorder;
public List<int> coords;
public Zone(int firstCoord) {
this.coords = new List<int>();
this.coords.Add(firstCoord);
this.isOnChunkBorder = false;
}
public void AddCoord(int coord) {
this.coords.Add(coord);
}
public void SetBool() {
this.isOnChunkBorder = true;
}
public Zone SetBoolAndReturnThis() {
this.isOnChunkBorder = true;
return this;
}
}
}
我不知道为什么当我使用简单的更新时结构布尔值没有更新,但是工作正常是 Zone 被类替换还是返回结构
您
观察到的由结构Zone
引起的效果。
结构是值类型,在赋值时复制。