C#繼承IList?接口的實(shí)現(xiàn)步驟
C#中的IList<T>接口是.NET框架中的一種通用接口,它定義了一組在運(yùn)行時(shí)可以使用類型參數(shù)T的元素的集合。IList<T>接口提供了添加、刪除和查找元素的方法,以及訪問(wèn)和操作列表中元素的索引的屬性。要實(shí)現(xiàn)IList<T>接口,可以按照以下步驟進(jìn)行:
1.聲明一個(gè)類,并實(shí)現(xiàn)IList<T>接口
聲明一個(gè)類,并實(shí)現(xiàn)IList<T>接口。在類的聲明中,使用: IList<T>語(yǔ)法來(lái)指定接口的實(shí)現(xiàn)。例如:
using System.Collections.Generic; public class MyList<T> : IList<T> { // 實(shí)現(xiàn)IList<T>接口的方法和屬性 }
2.實(shí)現(xiàn)IList<T>接口的屬性
IList<T>接口有兩個(gè)屬性:Count和IsReadOnly。Count屬性返回列表中元素的數(shù)量,IsReadOnly屬性返回一個(gè)值,表示列表是否為只讀。例如:
private List<T> list = new List<T>(); public int Count { get { return list.Count; } } public bool IsReadOnly { get { return false; } }
3.實(shí)現(xiàn)IList<T>接口的方法
IList<T>接口定義了許多方法,包括添加、刪除、查找和替換元素的方法。以下是部分方法的實(shí)現(xiàn)示例:
public void Add(T item) { list.Add(item); } public void Clear() { list.Clear(); } public bool Contains(T item) { return list.Contains(item); } public int IndexOf(T item) { return list.IndexOf(item); } public void Insert(int index, T item) { list.Insert(index, item); } public bool Remove(T item) { return list.Remove(item); } public void RemoveAt(int index) { list.RemoveAt(index); }
4.實(shí)現(xiàn)IList<T>接口的索引器
IList<T>接口定義了一個(gè)索引器,它允許通過(guò)索引訪問(wèn)列表中的元素。索引器的實(shí)現(xiàn)如下:
public T this[int index] { get { return list[index]; } set { list[index] = value; } }
5.主程序設(shè)計(jì)
在類的構(gòu)造函數(shù)中,可以初始化列表,并添加一些示例數(shù)據(jù)。例如:
public MyList() { // 初始化列表,并添加一些示例數(shù)據(jù) list.Add(1); list.Add("Hello"); list.Add(3.14); }
完成以上步驟后,就可以創(chuàng)建一個(gè)實(shí)現(xiàn)了IList<T>接口的類。這個(gè)類可以在其他代碼中作為列表使用,例如:
MyList<int> myList = new MyList<int>(); myList.Add(4); myList[1] = 2;
6.完整的實(shí)例
以下是一個(gè)實(shí)現(xiàn)了IList<T>接口的完整示例。在這個(gè)示例中,將創(chuàng)建一個(gè)名為MyList<T>的類,該類實(shí)現(xiàn)了IList<T>接口,并使用整數(shù)類型作為示例:
// IList<T>接口設(shè)計(jì)完整例子 using System.Collections; namespace _121_1 { public class MyList<T> : IList<T> { private readonly List<T> list = []; public int Count { get { return list.Count; } } public bool IsReadOnly { get { return false; } } public void Add(T item) { list.Add(item); } public void Clear() { list.Clear(); } public bool Contains(T item) { return list.Contains(item); } public int IndexOf(T item) { return list.IndexOf(item); } public void Insert(int index, T item) { list.Insert(index, item); } public bool Remove(T item) { return list.Remove(item); } public void RemoveAt(int index) { list.RemoveAt(index); } public void CopyTo(T[] array, int arrayIndex) { throw new NotImplementedException(); } //一個(gè)類必須實(shí)現(xiàn)它派生的接口的所有成員,否則將被聲明abstract //注釋部分是錯(cuò)誤的迭代器設(shè)計(jì),不能不設(shè)計(jì),注釋后警告CS0535 //public IEnumerator<T> GetEnumerator() //{ // throw new NotImplementedException(); //} //IEnumerator IEnumerable.GetEnumerator() //{ // throw new NotImplementedException(); //} /// <summary> /// 最重要的迭代器接口設(shè)計(jì) /// </summary> public IEnumerator<T> GetEnumerator() { for (int i = 0; i < list.Count; i++) { yield return list[i]; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public T this[int index] { get { return list[index]; } set { list[index] = value; } } /// <summary> /// list.Add(1),警告警告CS1503無(wú)法將int轉(zhuǎn)為T /// 必須經(jīng)如下?lián)Q后 /// </summary> public MyList() { // 初始化列表,并添加一些示例數(shù)據(jù) list.Add((T)(object)1); // 將1強(qiáng)制轉(zhuǎn)換為T類型 list.Add((T)(object)2); // 將2強(qiáng)制轉(zhuǎn)換為T類型 list.Add((T)(object)3); // 將3強(qiáng)制轉(zhuǎn)換為T類型 } } class Program { static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); MyList<int> myList = []; Console.WriteLine("初始列表: {0}", string.Join(", ", myList)); myList.Add(4); Console.WriteLine("添加元素4后的列表: {0}", string.Join(", ", myList)); myList[1] = 8; Console.WriteLine("將索引1處的元素替換為2后的列表: {0}", string.Join(", ", myList)); Console.ReadKey(); } } } // 運(yùn)行結(jié)果: /* 初始列表: 1, 2, 3 添加元素4后的列表: 1, 2, 3, 4 將索引1處的元素替換為2后的列表: 1, 8, 3, 4 */
7.繼承接口的迭代器設(shè)計(jì)一般步驟
迭代器由兩部分組成:
public IEnumerator<T> GetEnumerator() { // }
和
IEnumerator IEnumerable.GetEnumerator() { // }
要實(shí)現(xiàn)這個(gè)方法,需要?jiǎng)?chuàng)建一個(gè)枚舉器類,該類實(shí)現(xiàn)IEnumerator<T>接口,并使用IEnumerator<T>接口定義的Current屬性和MoveNext()方法。以下是一個(gè)簡(jiǎn)單的示例:
// 迭代器設(shè)計(jì),一般方法 using System.Collections; namespace _121_2 { internal class Program { private static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); List<int> list = [1, 2, 3, 4, 5]; MyEnumerable<int> myEnumerable1 = new(list); MyEnumerable<int> myEnumerable = myEnumerable1; foreach (int item in myEnumerable) { Console.WriteLine(item); } } } /// <summary> /// 迭代器設(shè)計(jì) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> public class MyEnumerable<T>(List<T> list) : IEnumerable<T> { private readonly List<T> _list = list; public IEnumerator<T> GetEnumerator() { for (int i = 0; i < _list.Count; i++) { yield return _list[i]; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } } //運(yùn)行結(jié)果: /* 1 2 3 4 5 */
在這個(gè)示例中,MyEnumerable類實(shí)現(xiàn)了IEnumerable<T>接口,并定義了一個(gè)GetEnumerator()方法,該方法返回一個(gè)枚舉器對(duì)象。枚舉器對(duì)象使用yield關(guān)鍵字返回列表中的每個(gè)元素。還有一個(gè)非泛型的GetEnumerator()方法,它調(diào)用泛型版本的GetEnumerator()方法,以滿足IEnumerable接口的要求。
要使用這個(gè)類,可以創(chuàng)建一個(gè)MyEnumerable<T>對(duì)象,并傳遞一個(gè)List<T>對(duì)象。再使用foreach循環(huán)遍歷MyEnumerable對(duì)象,并輸出列表中的每個(gè)元素。如上例主程序設(shè)計(jì)的那樣。
以上就是C#繼承IList 接口的實(shí)現(xiàn)步驟的詳細(xì)內(nèi)容,更多關(guān)于C#繼承IList 接口的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#使用foreach語(yǔ)句簡(jiǎn)單遍歷數(shù)組的方法
這篇文章主要介紹了C#使用foreach語(yǔ)句簡(jiǎn)單遍歷數(shù)組的方法,涉及C#中foreach語(yǔ)句的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#中類與結(jié)構(gòu)的區(qū)別實(shí)例分析
這篇文章主要介紹了C#中類與結(jié)構(gòu)的區(qū)別,類與結(jié)構(gòu)是C#初學(xué)者比較輕易混淆的概念,本文加以實(shí)例說(shuō)明,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)的簡(jiǎn)單隨機(jī)數(shù)產(chǎn)生器功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)的簡(jiǎn)單隨機(jī)數(shù)產(chǎn)生器功能,涉及C#簡(jiǎn)單界面布局、事件響應(yīng)及隨機(jī)數(shù)生成相關(guān)操作技巧,需要的朋友可以參考下2017-09-09一看就懂:圖解C#中的值類型、引用類型、棧、堆、ref、out
這篇文章主要介紹了一看就懂:圖解C#中的值類型、引用類型、棧、堆、ref、out,本文用淺顯易懂的語(yǔ)言組織介紹了這些容易混淆的概念,需要的朋友可以參考下2015-06-06C#實(shí)現(xiàn)在前端網(wǎng)頁(yè)彈出警告對(duì)話框(alert)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在前端網(wǎng)頁(yè)彈出警告對(duì)話框(alert)的方法,涉及C#通過(guò)自定義函數(shù)調(diào)用window.alert方法彈出對(duì)話框的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04