LinkedList 的只读包装。怎么做?

发布于 2024-08-12 11:27:35 字数 133 浏览 4 评论 0原文

.NET v2

当 List 有一个非常有用的(4 me)方法 AsReadOnly() LinkedList没有这样的方法。

有没有一种方法可以“快速”连接内部LinkedList以仅从外部代码读取?

.NET v2

When the List has a very useful (4 me) method AsReadOnly()
the LinkedList does not have such a method.

Is there a way to "quickly" interface an internal LinkedList to read only from the external code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沧笙踏歌 2024-08-19 11:27:35

为什么不直接返回 IEnumerable?如果您只想让用户枚举列表而不修改它*,IEnumerable 是显而易见的选择。

如果你想拥有 LinkedList 接口的只读接口,你可以包装 LinkedList,将只读方法转发到包装列表并拒绝任何更改。

*)请记住,在引用类型集合的情况下,ReadOnlyCollection 和 IEnumerable 都不会阻止调用者更改对象的状态。如果对象也应该是只读的,则需要将其实现为对象类型的一部分。

Why not just return a IEnumerable<T>? If you just want to let users enumerate the list without modifying it*, IEnumerable is the obvious choice.

If you want to have a read only interface of the LinkedList interface, you can wrap LinkedList, forward read only methods to the wrapped list and deny any changes.

*) Keep in mind that neither ReadOnlyCollection not IEnumerable will prevent callers to change state of objects in case of a collection of reference types. If the objects should be read only as well, you need to implement this as part of their type.

雨巷深深 2024-08-19 11:27:35

ReadOnlyCollectionIList 作为构造函数的参数。 LinkedList 未实现此接口。 List 具有一个构造函数重载,该重载采用 IEnumerable 作为参数,并且 LinkedList 实现此接口。因此,以下内容应该有效:

LinkedList<int> linked = new LinkedList<int>();
// populate the list

ReadOnlyCollection<int> readOnly = new ReadOnlyCollection<int>(
    new List<int>(linked));

它使用 List 实例将项目携带到 ReadOnlyCollection 构造函数中。

ReadOnlyCollection<T> takes an IList<T> as argument to the constructor. LinkedList<T> does not implement this interface. List<T> has a constructor overload that takes an IEnumerable<T> as argument, and LinkedList<T> implements this interface. So the following should work:

LinkedList<int> linked = new LinkedList<int>();
// populate the list

ReadOnlyCollection<int> readOnly = new ReadOnlyCollection<int>(
    new List<int>(linked));

It uses an instance of List<T> to carry the items into the ReadOnlyCollection<T> constructor.

苏辞 2024-08-19 11:27:35

#1 修复链接列表

class FixedLinkedList<T> : LinkedList<T>, IList<T>
{
    public T this[int index]
    {
        get
        {
            return GetAt(index).Value;
        }
        set
        {
            GetAt(index).Value = value;
        }
    }

    public int IndexOf(T item)
    {
        int i = 0;
        var node = First;
        while (node!= null && !node.Value.Equals(item))
        {
            i++;
            node = node.Next;
        }

        if (node == null)
        {
            return -1;
        }

        return i;
    }

    public void Insert(int index, T item)
    {
        AddBefore(GetAt(index), new LinkedListNode<T>(item));
    }


    public void RemoveAt(int index)
    {
        Remove(GetAt(index));
    }

    private LinkedListNode<T> GetAt(int index)
    {
        CheckIndex(index);
        int i = 0;
        var node = First;
        while (i < index)
        {
            i++;
            node = node.Next;
        }

        return node;
    }

    private void CheckIndex(int index)
    {
        if (index < 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(index),
                "Parameter must be greater or equal to 0.");
        }

        if (index >= Count)
        {
            throw new ArgumentOutOfRangeException(
                nameof(index),
                "Parameter must be lower than the list item Count.");
        }
    }
}

#2 将其实例包装在 ReadOnlyCollection 中

#1 Fix the Linked List

class FixedLinkedList<T> : LinkedList<T>, IList<T>
{
    public T this[int index]
    {
        get
        {
            return GetAt(index).Value;
        }
        set
        {
            GetAt(index).Value = value;
        }
    }

    public int IndexOf(T item)
    {
        int i = 0;
        var node = First;
        while (node!= null && !node.Value.Equals(item))
        {
            i++;
            node = node.Next;
        }

        if (node == null)
        {
            return -1;
        }

        return i;
    }

    public void Insert(int index, T item)
    {
        AddBefore(GetAt(index), new LinkedListNode<T>(item));
    }


    public void RemoveAt(int index)
    {
        Remove(GetAt(index));
    }

    private LinkedListNode<T> GetAt(int index)
    {
        CheckIndex(index);
        int i = 0;
        var node = First;
        while (i < index)
        {
            i++;
            node = node.Next;
        }

        return node;
    }

    private void CheckIndex(int index)
    {
        if (index < 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(index),
                "Parameter must be greater or equal to 0.");
        }

        if (index >= Count)
        {
            throw new ArgumentOutOfRangeException(
                nameof(index),
                "Parameter must be lower than the list item Count.");
        }
    }
}

#2 Wraps its instances in ReadOnlyCollection

请爱~陌生人 2024-08-19 11:27:35

LinkedList 没有实现 IList 所以简而言之,没有办法快速做到这一点。如果将 LinkedList 转换为列表,您可能会失去所需的功能。

LinkedList 不为您提供任何子类化功能,因此您需要编写自己的。我想下一个请求是“你能告诉我该怎么做吗”

LinkedList<T> doesn't implement IList so in short, no there is no way of quickly doing it. If you cast your LinkedList to a List you will probably lose the functionality you require.

LinkedList<T> doesn't offer you anything for subclassing so you'll need to write your own. I imagine the next request is "can you show me how to do that"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文