Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 什么';让对象知道其在列表中的位置的最佳方法是什么?_C#_List - Fatal编程技术网

C# 什么';让对象知道其在列表中的位置的最佳方法是什么?

C# 什么';让对象知道其在列表中的位置的最佳方法是什么?,c#,list,C#,List,我有一个对象,保存在某个列表中。然而,对象本身应该知道他在一个列表中,并且在这个列表中有邻居。使用此对象的其他实体虽然不必知道整个列表,但仍然需要知道对象的邻居,甚至在对象的邻居发生变化时得到通知 一个简单的例子是保存图像列表并在屏幕上显示,旁边有“上一个”和“下一个”按钮,而源列表会自动改变背景 比如说,在这个例子中,我有这样的东西: public class ImageListController { private List<Image> m_ImageList = n

我有一个对象,保存在某个列表中。然而,对象本身应该知道他在一个列表中,并且在这个列表中有邻居。使用此对象的其他实体虽然不必知道整个列表,但仍然需要知道对象的邻居,甚至在对象的邻居发生变化时得到通知

一个简单的例子是保存图像列表并在屏幕上显示,旁边有“上一个”和“下一个”按钮,而源列表会自动改变背景

比如说,在这个例子中,我有这样的东西:

public class ImageListController
{
    private List<Image> m_ImageList = new List<Image>();

    // ... some code changing the list

}

public class ImageView
{
    // ...
    private Image m_Image;
    private Button m_Next;
    private Button m_Previous;

    void OnImageUpdate() // subscribed to events from m_Image
    {
        if (m_Image.Previous != null)
        {
            m_Previous.Enabled = true;
            m_Previous.Action = (System.Action) (() => DisplayImage(m_Image.Previous));
        }
        else
        {
            m_Previous.Enabled = false;
        }
    }
}
公共类ImageListController
{
私有列表m_ImageList=新列表();
//…一些更改列表的代码
}
公共类ImageView
{
// ...
私有镜像m_镜像;
私人按钮m_Next;
私人按钮m_-Previous;
void OnImageUpdate()//订阅了来自m_映像的事件
{
if(m_Image.Previous!=null)
{
m_Previous.Enabled=真;
m_Previous.Action=(System.Action)(()=>DisplayImage(m_Image.Previous));
}
其他的
{
m_Previous.Enabled=错误;
}
}
}
实现这一点的最佳方式是什么

(当然,我可以自己从头开始实现这个功能,但我怀疑已经存在某种内置的集合类型用于此目的。)

让对象知道其在列表中的位置的最佳方法是什么


对象不应该知道它是在列表、数组、字段还是本地。如果同一个对象在多个地方被引用,您将如何解决这个问题?这对我来说毫无意义


听起来你在寻找,如果你有了这个节点,你就可以使用和属性来找到它的朋友。

你目前拥有的一些代码可能是useful@Tanner我可以写下一些示例代码,但它对这个特定的问题有什么帮助?添加了示例代码,我希望它能澄清我在这里要做的事情“如果同一个对象在多个位置被引用,您将如何处理?”我只需要在特定的特殊列表上下文中了解他的邻居。我考虑了
LinkedList
,但
LinkedListNode
没有关于邻居节点更改的事件。