C # generics and Arrays in C # 2.0, a one-dimensional array with a lower bound of zero automatically implements Ilist<t>. This allows you to create generic methods that can iterate through arrays and other collection types using the same code. This technique is primarily useful for reading data in a collection. The Ilist<t> interface cannot be used to add or remove elements from an array, and an exception is thrown if an attempt is made to call the Ilist<t> method in this context, such as the removeat of a group. The following code example shows how a single generic method with ilist<t> input parameters can iterate through the list and array at the same time, in this case, an integer.
C # Generic and array code
Copy Code code as follows:
Class Program
{
static void Main ()
{
Int[] arr = {0, 1, 2, 3, 4};
list<int> list = new list<int> ();
for (int x = 5; x < X. x + +)
{
List. ADD (x);
}
Processitems<int> (arr);
Processitems<int> (list);
}
static void Processitems<t> (Ilist<t> coll)
{
foreach (T item in coll)
{
System.Console.Write (item. ToString () + "");
}
System.Console.WriteLine ();
}
}
Note for C # generics and array applications
Although the ProcessItems method cannot add or remove an item, it returns FALSE for the T[],isreadonly property inside ProcessItems because the array itself does not declare the ReadOnly attribute.
The relevant content of C # generics and arrays is introduced to you here, hoping to help you understand and learn C # generics and arrays.