---发布后更新了第一个代码---
上发现的许多错误我在使用布尔对象时遇到了一些麻烦。 这是我的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;
}
}
}
我不知道为什么在使用简单更新时没有更新struct boolean,但是如果Zone被Class替换或者返回结构,那么工作正常吗?