C # Generic Array Learning Summary _c# tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.