📜  演示 IList 接口的 C# 程序

📅  最后修改于: 2022-05-13 01:54:24.236000             🧑  作者: Mango

演示 IList 接口的 C# 程序

在 C# 中,IList 接口是属于集合模块的接口,我们可以通过索引访问每个元素。或者我们可以说它是一个对象的集合,用于在索引的帮助下单独访问每个元素。它是通用类型和非通用类型。它的实现通常分为三个部分:

  • 只读:不能修改 IList。
  • 固定大小:不允许 IList 在其中添加或删除元素。
  • 可变大小:允许 IList 在其中修改、添加或删除元素。

句法:

// For Non_generic IList
public interface IList : System.Collections.ICollection

// For Generic IList
public interface IList : System.Collections.Generic.ICollection,
                            System.Collections.Generic.IEnumerable

特性:

IList 接口的一些常用属性有:

Property NameDescription
CountThis property will return the total number of items present in the ICollection.
IsFixedSizeThis property will return a value that determines whether the IList has a fixed size or not.
IsReadOnlyThis property will return a value that determines whether the IList is read-only or not.
IsSynchronizedThis property will return a value that determines whether access to the ICollection is synchronized or not.
Item[Int32]This property will gets or sets the item from the specified index.
SyncRootThis property will get an object that can be used to synchronize access to the ICollection.

方法:

IList 接口的一些常用方法有:

Method NameDescription
Add(Object)This method is used to add an item to the IList.
Clear()This method is used to remove or delete all items from the IList.
Contains(Object)This method is used to check whether the IList contains a specific value or not.
CopyTo(Array, Int32)This method is used to copy the elements of the ICollection to an array, starting from the given index.
GetEnumerator()This method is used to get an enumerator that iterates through a collection.
IndexOf(Object)This method is used to find the index of a specific item in the IList.
Insert(Int32, Object)This method is used to insert an item to the IList at the given index.
Remove(Object)This method is used to remove or eliminate the first occurrence of a given object from the IList.
RemoveAt(Int32)This method is used to remove the IList item from the given index.

现在我们通过一个示例了解 IList 接口的工作原理。在下面的示例中,我们创建了一个包含科目和学生的列表并显示该列表。所以要做到这一点,我们必须遵循以下方法:

方法:

1.创建主题数组

{"os","cn","php","c/cpp","java/jsp","python/r"}

2.创建学生姓名列表并使用 Add() 方法将姓名添加到其中。

List data = new List();
data.Add("sai");
data.Add("sravan");
data.Add("jyothika");

3.定义一个 IList 界面来显示学生和科目名称。

static void Show(IList list)
{
    // Iterate through list
    foreach (string str in list)
    {
        // Print
        Console.WriteLine("\t" + str);
    }
 }

4.调用 Show() 方法显示学生和科目列表。

Show(subjects);
Show(data);

例子:

C#
// C# program to illustrate the IList interface
using System;
using System.Collections.Generic;
  
class GFG{
  
// Show method to display the list of
// subject and students name
static void Show(IList list)
{
      
    // Iterate through the list
    foreach(string str in list)
    {
        Console.WriteLine("\t" + str);
    }
}
  
// Driver code
static void Main()
{
      
    // Declare a list of subjects
    string[] subjects = { "OS", "CN", "PHP", "C/CPP", 
                          "Java/Jsp", "Python/R" };
  
    // Define  a list
    List data = new List();
  
    // Add students to the list
    data.Add("sai");
    data.Add("sravan");
    data.Add("jyothika");
      
    // Display subjects
    Console.WriteLine("Subjects Name: ");
    Show(subjects);
      
    // Display students
    Console.WriteLine("Students Name: ");
    Show(data);
}
}


输出:

Subjects Name: 
    OS
    CN
    PHP
    C/CPP
    Java/Jsp
    Python/R
Students Name: 
    sai
    sravan
    jyothika