英語で読む

次の方法で共有


IList インターフェイス

定義

インデックスによって個別にアクセスできるオブジェクトの非ジェネリック コレクションを表します。

C#
public interface IList : System.Collections.ICollection
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface IList : System.Collections.ICollection
派生
属性
実装

次の例では、単純な固定サイズのリストを作成する IList インターフェイスの実装を示します。

C#
using System;
using System.Collections;

class Program
{
    static void Main()
    {
        var test = new SimpleList();

        // Populate the List.
        Console.WriteLine("Populate the List");
        test.Add("one");
        test.Add("two");
        test.Add("three");
        test.Add("four");
        test.Add("five");
        test.Add("six");
        test.Add("seven");
        test.Add("eight");
        test.PrintContents();
        Console.WriteLine();

        // Remove elements from the list.
        Console.WriteLine("Remove elements from the list");
        test.Remove("six");
        test.Remove("eight");
        test.PrintContents();
        Console.WriteLine();

        // Add an element to the end of the list.
        Console.WriteLine("Add an element to the end of the list");
        test.Add("nine");
        test.PrintContents();
        Console.WriteLine();

        // Insert an element into the middle of the list.
        Console.WriteLine("Insert an element into the middle of the list");
        test.Insert(4, "number");
        test.PrintContents();
        Console.WriteLine();

        // Check for specific elements in the list.
        Console.WriteLine("Check for specific elements in the list");
        Console.WriteLine($"List contains \"three\": {test.Contains("three")}");
        Console.WriteLine($"List contains \"ten\": {test.Contains("ten")}");
    }
}

class SimpleList : IList
{
    private object[] _contents = new object[8];
    private int _count;

    public SimpleList()
    {
        _count = 0;
    }

    // IList Members
    public int Add(object value)
    {
        if (_count < _contents.Length)
        {
            _contents[_count] = value;
            _count++;

            return (_count - 1);
        }

        return -1;
    }

    public void Clear()
    {
        _count = 0;
    }

    public bool Contains(object value)
    {
        for (int i = 0; i < Count; i++)
        {
            if (_contents[i] == value)
            {
                return true;
            }
        }
        return false;
    }

    public int IndexOf(object value)
    {
        for (int i = 0; i < Count; i++)
        {
            if (_contents[i] == value)
            {
                return i;
            }
        }
        return -1;
    }

    public void Insert(int index, object value)
    {
        if ((_count + 1 <= _contents.Length) && (index < Count) && (index >= 0))
        {
            _count++;

            for (int i = Count - 1; i > index; i--)
            {
                _contents[i] = _contents[i - 1];
            }
            _contents[index] = value;
        }
    }

    public bool IsFixedSize
    {
        get
        {
            return true;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public void Remove(object value)
    {
        RemoveAt(IndexOf(value));
    }

    public void RemoveAt(int index)
    {
        if ((index >= 0) && (index < Count))
        {
            for (int i = index; i < Count - 1; i++)
            {
                _contents[i] = _contents[i + 1];
            }
            _count--;
        }
    }

    public object this[int index]
    {
        get
        {
            return _contents[index];
        }
        set
        {
            _contents[index] = value;
        }
    }

    // ICollection members.

    public void CopyTo(Array array, int index)
    {
        for (int i = 0; i < Count; i++)
        {
            array.SetValue(_contents[i], index++);
        }
    }

    public int Count
    {
        get
        {
            return _count;
        }
    }

    public bool IsSynchronized
    {
        get
        {
            return false;
        }
    }

    // Return the current instance since the underlying store is not
    // publicly available.
    public object SyncRoot
    {
        get
        {
            return this;
        }
    }

    // IEnumerable Members

    public IEnumerator GetEnumerator()
    {
        // Refer to the IEnumerator documentation for an example of
        // implementing an enumerator.
        throw new NotImplementedException("The method or operation is not implemented.");
    }

    public void PrintContents()
    {
        Console.WriteLine($"List has a capacity of {_contents.Length} and currently has {_count} elements.");
        Console.Write("List contents:");
        for (int i = 0; i < Count; i++)
        {
            Console.Write($" {_contents[i]}");
        }
        Console.WriteLine();
    }
}

// This code produces output similar to the following:
// Populate the List:
// List has a capacity of 8 and currently has 8 elements.
// List contents: one two three four five six seven eight
//
// Remove elements from the list:
// List has a capacity of 8 and currently has 6 elements.
// List contents: one two three four five seven
//
// Add an element to the end of the list:
// List has a capacity of 8 and currently has 7 elements.
// List contents: one two three four five seven nine
//
// Insert an element into the middle of the list:
// List has a capacity of 8 and currently has 8 elements.
// List contents: one two three four number five seven nine
//
// Check for specific elements in the list:
// List contains "three": True
// List contains "ten": False

注釈

IList は、ICollection インターフェイスの子孫であり、すべての非ジェネリック リストの基本インターフェイスです。 IList 実装は、読み取り専用、固定サイズ、可変サイズの 3 つのカテゴリに分類されます。 読み取り専用 IList は変更できません。 固定サイズの IList では、要素を追加または削除することはできませんが、既存の要素を変更できます。 可変サイズの IList により、要素の追加、削除、および変更が可能になります。

このインターフェイスの汎用バージョンについては、System.Collections.Generic.IList<T>を参照してください。

プロパティ

Count

ICollectionに含まれる要素の数を取得します。

(継承元 ICollection)
IsFixedSize

IList に固定サイズがあるかどうかを示す値を取得します。

IsReadOnly

IList が読み取り専用かどうかを示す値を取得します。

IsSynchronized

ICollection へのアクセスが同期されているかどうかを示す値を取得します (スレッド セーフ)。

(継承元 ICollection)
Item[Int32]

指定したインデックス位置にある要素を取得または設定します。

SyncRoot

ICollectionへのアクセスを同期するために使用できるオブジェクトを取得します。

(継承元 ICollection)

メソッド

Add(Object)

IListに項目を追加します。

Clear()

IListからすべての項目を削除します。

Contains(Object)

IList に特定の値が含まれているかどうかを判断します。

CopyTo(Array, Int32)

特定の Array インデックスから始まる ICollection の要素を Arrayにコピーします。

(継承元 ICollection)
GetEnumerator()

コレクションを反復処理する列挙子を返します。

(継承元 IEnumerable)
IndexOf(Object)

IList内の特定の項目のインデックスを決定します。

Insert(Int32, Object)

指定したインデックス位置にある IList に項目を挿入します。

Remove(Object)

特定のオブジェクトの最初の出現箇所を IListから削除します。

RemoveAt(Int32)

指定したインデックス位置にある IList 項目を削除します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定した型に基づいて、IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryableに変換します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください