C# 如何在C中创建引用集合#

C# 如何在C中创建引用集合#,c#,collections,reference,C#,Collections,Reference,好的,我有一个跨语言的呃逆。在C#中,它有很多很棒的收藏,比如列表和 我有: 属性为: 列表图像 列出瓷砖 瓦片类: byte[]图像数据 int X int-Y 现在我想向Map类添加一个图像,并使用Tile类的ImageData属性来“引用”它。我发现我不能只为它分配图像[0]。不能有对列表对象的引用 我的解决办法是编一本字典。这是最好的方法吗?还是我可以有一个指向对象集合的“指针” 作为对注释的响应,如果我有一堆分幅,其中imagedata指向列表中的第一个字节数组,然后我在列表中更改该

好的,我有一个跨语言的呃逆。在C#中,它有很多很棒的收藏,比如列表和 我有:

属性为:
列表
图像
列出
瓷砖

瓦片类:
byte[]图像数据
int X
int-Y

现在我想向Map类添加一个图像,并使用Tile类的ImageData属性来“引用”它。我发现我不能只为它分配图像[0]。不能有对列表对象的引用

我的解决办法是编一本字典。这是最好的方法吗?还是我可以有一个指向对象集合的“指针”

  • 作为对注释的响应,如果我有一堆分幅,其中imagedata指向列表中的第一个字节数组,然后我在列表中更改该图像,并在分幅中抓取图像,它们是以前的图像分配。因此,将瓷砖分配给列表中的第一个图像,然后更改该图像,我现在希望瓷砖引用新图像。他们没有
最终修订版-看看gladford3x的代码(我还没有掌握格式)。Main中的最后一行将是
myMap.Images[0]=image2

当您调用myMap.Tiles[0].ImageData时,它将拥有来自第一个字节数组的数据

我认为这是正确的方法

在Map类中使用命名集合

public class Map 
{
    public List<byte[]> Images;
    public Dictionary<int, Tile> Tiles;//you could use an int or a string to name the item
}

这就是你在做的吗?

我认为这是正确的选择

在Map类中使用命名集合

public class Map 
{
    public List<byte[]> Images;
    public Dictionary<int, Tile> Tiles;//you could use an int or a string to name the item
}
这就是您正在做的吗?

现在我想向Map类添加一个图像,并使用Tile类的ImageData属性来“引用”它

List List=新列表();
添加(newmap());
列表[0]。ImageData=新字节[];
//稍后访问列表时,将引用同一地图对象。
它是引用类型,因此将进行更新。您可以复制列表,原始引用仍将更新。该类拥有一个值类型(您的字节数组
ImageData
),但是它存储在堆上,因此您不必担心指针管理。

现在我想向Map类添加一个图像,并将平铺类的ImageData属性“引用”它

List List=新列表();
添加(newmap());
列表[0]。ImageData=新字节[];
//稍后访问列表时,将引用同一地图对象。

它是引用类型,因此将进行更新。您可以复制列表,原始引用仍将更新。该类保存一个值类型(字节数组
ImageData
),但是它存储在堆上,因此您不必担心指针管理。

我不确定是否完全理解。我把这个问题读了好几遍,下面是我的想法。以下内容对我来说似乎很好。我建议使用它作为概念证明,并实现适当的接口,如ICollection、IEnumerator、IList或满足您需要的任何接口


编辑:

static void Main(string[] args) 
{
    Map myMap = new Map(); 
    myMap.Images = new List<byte[]>(); 
    myMap.Tiles = new List<Tile>(); 

    byte[] image = new byte[] { 1, 2, 3 }; 
    byte[] image2 = new byte[] { 10, 20, 30 }; 
    myMap.Add(image);
    myMap.Add(image2);

    byte[] image3 = new byte[] {100,200,255};
    myMap[0]=image3;

    printallmaps(myMap);
    myMap.Tiles.ForEach(c=>printall(c));
} 


public class Map 
{
    public List<byte[]> Images  { get; set; } 
    public byte[] this[int i]
    {
        get
        {
            return Tiles[i].ImageData;
        }
        set
        {
            if(i >= this.Count)
            {
                this.Insert(i,value);
            }
            else
            {
                Tiles[i].ImageData = value;
            }
        }
    }
    public List<Tile> Tiles { get; set; }
    public int Count {get {return Tiles.Count;}}
    public void Add(byte[] image)
    {
        this[this.Count] = image;
    }
    public void Insert(int x, byte[] image)
    {
        Tile tile = new Tile();
        tile.ImageData = image;
        Tiles.Insert(x,tile);
    }
} 

public class Tile 
{ 
    public byte[] ImageData; 
    int x; 
    int y; 
} 
static void Main(字符串[]args)
{
Map myMap=newmap();
myMap.Images=新列表();
myMap.Tiles=新列表();
字节[]图像=新字节[]{1,2,3};
字节[]图像2=新字节[]{10,20,30};
添加(图片);
myMap.Add(图2);
字节[]图像3=新字节[]{100200255};
myMap[0]=image3;
打印所有地图(myMap);
ForEach(c=>printall(c));
} 
公共类地图
{
公共列表图像{get;set;}
公共字节[]此[int i]
{
得到
{
返回分片[i]。图像数据;
}
设置
{
如果(i>=this.Count)
{
本条。插入(i,价值);
}
其他的
{
分幅[i]。图像数据=值;
}
}
}
公共列表分幅{get;set;}
公共int计数{get{return Tiles.Count;}
公共无效添加(字节[]图像)
{
this[this.Count]=图像;
}
公共无效插入(整数x,字节[]图像)
{
瓷砖=新瓷砖();
tile.ImageData=图像;
瓷砖。插入(x,瓷砖);
}
} 
公共级瓷砖
{ 
公共字节[]图像数据;
int x;
int-y;
} 

我不确定自己是否完全理解。我把这个问题读了好几遍,下面是我的想法。以下内容对我来说似乎很好。我建议使用它作为概念证明,并实现适当的接口,如ICollection、IEnumerator、IList或满足您需要的任何接口


编辑:

static void Main(string[] args) 
{
    Map myMap = new Map(); 
    myMap.Images = new List<byte[]>(); 
    myMap.Tiles = new List<Tile>(); 

    byte[] image = new byte[] { 1, 2, 3 }; 
    byte[] image2 = new byte[] { 10, 20, 30 }; 
    myMap.Add(image);
    myMap.Add(image2);

    byte[] image3 = new byte[] {100,200,255};
    myMap[0]=image3;

    printallmaps(myMap);
    myMap.Tiles.ForEach(c=>printall(c));
} 


public class Map 
{
    public List<byte[]> Images  { get; set; } 
    public byte[] this[int i]
    {
        get
        {
            return Tiles[i].ImageData;
        }
        set
        {
            if(i >= this.Count)
            {
                this.Insert(i,value);
            }
            else
            {
                Tiles[i].ImageData = value;
            }
        }
    }
    public List<Tile> Tiles { get; set; }
    public int Count {get {return Tiles.Count;}}
    public void Add(byte[] image)
    {
        this[this.Count] = image;
    }
    public void Insert(int x, byte[] image)
    {
        Tile tile = new Tile();
        tile.ImageData = image;
        Tiles.Insert(x,tile);
    }
} 

public class Tile 
{ 
    public byte[] ImageData; 
    int x; 
    int y; 
} 
static void Main(字符串[]args)
{
Map myMap=newmap();
myMap.Images=新列表();
myMap.Tiles=新列表();
字节[]图像=新字节[]{1,2,3};
字节[]图像2=新字节[]{10,20,30};
添加(图片);
myMap.Add(图2);
字节[]图像3=新字节[]{100200255};
myMap[0]=image3;
打印所有地图(myMap);
ForEach(c=>printall(c));
} 
公共类地图
{
公共列表图像{get;set;}
公共字节[]此[int i]
{
得到
{
返回分片[i]。图像数据;
}
设置
{
如果(i>=this.Count)
{
本条。插入(i,价值);
}
其他的
{
分幅[i]。图像数据=值;
}
}
}
公共列表分幅{get;set;}
公共int计数{get{return Tiles.Count;}
公共无效添加(字节[]图像)
{
this[this.Count]=图像;
}
公共无效插入(整数x,字节[]图像)
{
瓷砖=新瓷砖();
tile.ImageData=图像;
瓷砖。插入(x,瓷砖);
}
} 
公共级瓷砖
{ 
公共字节[]图像数据;
int x;
int-y;
} 

这里的问题是,地图管理图像,但平铺使用它们。如果我理解正确的话,你不想让瓷砖知道地图,所以你更喜欢参考
public class TileImage
{
    public byte[] ImageData;
}

public class Tile
{
    public TileImage Image;
    int x;
    int y;
}