SoFunction
Updated on 2025-03-07

Implementation steps of C# inheriting IList interface

The IList<T> interface in C# is a common interface in the .NET framework that defines a set of elements that can use the type parameter T at runtime. The IList<T> interface provides methods to add, delete, and find elements, as well as access and manipulate attributes of indexes of elements in a list. To implement the IList<T> interface, you can follow the following steps:

1. Declare a class and implement the IList<T> interface

Declare a class and implement the IList<T> interface. In the class declaration, use the: IList<T> syntax to specify the implementation of the interface. For example:

using ;
public class MyList&lt;T&gt; : IList&lt;T&gt;
{
   // Methods and properties of IList<T> interface}

2. Implement the properties of the IList<T> interface

The IList<T> interface has two properties: Count and IsReadOnly. The Count property returns the number of elements in the list, and the IsReadOnly property returns a value indicating whether the list is read-only. For example:

private List<T> list = new List<T>();
 
public int Count
{
    get { return ; }
}
 
public bool IsReadOnly
{
    get { return false; }
}

3. Methods to implement IList<T> interface

The IList<T> interface defines many methods, including methods to add, delete, find and replace elements. Here are some implementation examples:

public void Add(T item)
{
    (item);
}
 
public void Clear()
{
    ();
}
 
public bool Contains(T item)
{
    return (item);
}
 
public int IndexOf(T item)
{
    return (item);
}
 
public void Insert(int index, T item)
{
    (index, item);
}
 
public bool Remove(T item)
{
    return (item);
}
 
public void RemoveAt(int index)
{
    (index);
}

4. Implement the indexer for IList<T> interface

The IList<T> interface defines an indexer that allows access to elements in a list through the index. The indexer is implemented as follows:

public T this[int index]
{
    get
    {
        return list[index];
    }
    set
    {
        list[index] = value;
    }
}

5. Main program design

In the class constructor, you can initialize the list and add some sample data. For example:

public MyList()
{
    // Initialize the list and add some sample data    (1);
    ("Hello");
    (3.14);
}

After completing the above steps, you can create a class that implements the IList<T> interface. This class can be used as a list in other code, for example:

MyList<int> myList = new MyList<int>();
(4);
myList[1] = 2;

6. Complete example

Here is a complete example that implements the IList<T> interface. In this example, a class named MyList<T> is created that implements the IList<T> interface and uses integer types as an example:

// Complete example of IList<T> interface designusing ;
 
namespace _121_1
{
    public class MyList&lt;T&gt; : IList&lt;T&gt;
    {
        private readonly List&lt;T&gt; list = [];
 
        public int Count
        {
            get { return ; }
        }
 
        public bool IsReadOnly
        {
            get { return false; }
        }
 
        public void Add(T item)
        {
            (item);
        }
 
        public void Clear()
        {
            ();
        }
 
        public bool Contains(T item)
        {
            return (item);
        }
 
        public int IndexOf(T item)
        {
            return (item);
        }
 
        public void Insert(int index, T item)
        {
            (index, item);
        }
 
        public bool Remove(T item)
        {
            return (item);
        }
 
        public void RemoveAt(int index)
        {
            (index);
        }
 
        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }
        //A class must implement all members of the interface it derived, otherwise it will be declared abstract        //The comment part is an incorrect iterator design, and it cannot be ignored. Warn CS0535 after commenting        //public IEnumerator&lt;T&gt; GetEnumerator()
        //{
        //    throw new NotImplementedException();
        //}
        //IEnumerator ()
        //{
        //    throw new NotImplementedException();
        //}
 
        /// &lt;summary&gt;
        /// The most important iterator interface design        /// &lt;/summary&gt;
        public IEnumerator&lt;T&gt; GetEnumerator()
        {
            for (int i = 0; i &lt; ; i++)
            {
                yield return list[i];
            }
        }
        IEnumerator ()
        {
            return GetEnumerator();
        }
 
        public T this[int index]
        {
            get
            {
                return list[index];
            }
            set
            {
                list[index] = value;
            }
        }
        /// &lt;summary&gt;
        /// (1), Warning CS1503 cannot convert int to T        /// Must be replaced as follows        /// &lt;/summary&gt;
        public MyList()
        {
            // Initialize the list and add some sample data            ((T)(object)1); // cast 1 to T type            ((T)(object)2); // cast 2 to T type            ((T)(object)3); // cast 3 to T type        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            (args);
 
            MyList&lt;int&gt; myList = [];
            ("Initial list: {0}", (", ", myList));
 
            (4);
            ("Add elements4The following list: {0}", (", ", myList));
 
            myList[1] = 8;
            ("To index1Replace the element at2The following list: {0}", (", ", myList));
 
            ();
        }
    }
}
// Running results:/*
 Initial list: 1, 2, 3
 List after adding element 4: 1, 2, 3, 4
 Replace the element at index 1 with the list after 2: 1, 8, 3, 4
  */

7. General steps for iterator design of inherited interfaces

The iterator consists of two parts:

public IEnumerator<T> GetEnumerator()
{
    //
}

and

 IEnumerator ()
{
    //
}

To implement this method, you need to create an enumerator class that implements the IEnumerator<T> interface and uses the Current attribute and MoveNext() method defined by the IEnumerator<T> interface. Here is a simple example:

// Iterator design, general methodusing ;
 
namespace _121_2
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            (args);
 
            List&lt;int&gt; list = [1, 2, 3, 4, 5];
            MyEnumerable&lt;int&gt; myEnumerable1 = new(list);
            MyEnumerable&lt;int&gt; myEnumerable = myEnumerable1;
 
            foreach (int item in myEnumerable)
            {
                (item);
            }
        }
    }
    /// &lt;summary&gt;
    /// Iterator design    /// &lt;/summary&gt;
    /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
    /// &lt;param name="list"&gt;&lt;/param&gt;
    public class MyEnumerable&lt;T&gt;(List&lt;T&gt; list) : IEnumerable&lt;T&gt;
    {
        private readonly List&lt;T&gt; _list = list;
 
        public IEnumerator&lt;T&gt; GetEnumerator()
        {
            for (int i = 0; i &lt; _list.Count; i++)
            {
                yield return _list[i];
            }
        }
 
        IEnumerator ()
        {
            return GetEnumerator();
        }
    }
}
//Run result:/*
1
2
3
4
5
 */

In this example, the MyEnumerable class implements the IEnumerable<T> interface and defines a GetEnumerator() method that returns an enumerator object. The enumerator object uses the yield keyword to return each element in the list. There is also a non-generic GetEnumerator() method, which calls the generic version of GetEnumerator() method to meet the requirements of the IEnumerable interface.

To use this class, you can create a MyEnumerable<T> object and pass a List<T> object. Then use foreach to loop through the MyEnumerable object and output each element in the list. As designed by the main program in the example above.

The above is the detailed content of the implementation steps of C# inheriting the IList interface. For more information about C# inheriting the IList interface, please pay attention to my other related articles!