Analysis of the difference and application of [],list,array,arraylist] in C #

Source: Internet
Author: User

[] is for a specific type, fixed length.

List is for a specific type, any length.

The Array is for a specific type, fixed length.

The ArrayList is for any type, any length.

Arrays and ArrayList are implemented in any type by storing object, so they are converted when used.

Application examples

Copy CodeThe code is as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Collections;

Public partial class _default:system.web.ui.page 

    protected void Page_Load ( Object sender, EventArgs e)  
    { 
       // System.Int32 is a structure  
        int[] arr = new int[] {1, 2, 3}; 
 &N bsp;      Response.Write (Arr[0]); 1 
        Change (arr);  
         Response.Write (Arr[0]); 2

The

       //List namespace is system.collections.generic 
         list<int> List = new list<int> ();  
         list. ADD (1);  
        list. ADD (2);  
        list. ADD (3);  
        Response.Write (list[0]);//1 
         change (list);  
        Response.Write ( List[0]); 2

The

       //Array namespace is system 
         Array array = array.createinstance (System.Type.GetType ("System.Int32"), 3); Array is an abstract class and cannot be created using the new array.  
        Array. SetValue (1, 0);  
        Array. SetValue (2, 1);  
        Array. SetValue (3, 2);  
        Response.Write (Array. GetValue (0)); 1 
        Change (array);  
         Response.Write (Array. GetValue (0)); 2

The

       //ArrayList namespace is system.collections 
         ArrayList ArrayList = new ArrayList (3);  
         Arraylist.add (1);  
        Arraylist.add (2);  
         Arraylist.add (3);  
        Response.Write (Arraylist[0]); 1 
        Change (arrayList);  
         Response.Write (Arraylist[0]); 2 
   }

private void Change (int[] arr)
{
for (int i = 0; i < arr. Length; i++)
{
Arr[i] *= 2;
}
}

private void Change (list<int> List)
{
for (int i = 0; i < list. Count; i++)//Use Count
{
List[i] *= 2;
}
}


private void Change (array array)
{
for (int i = 0; i < array. Length; i++)//Use Length
{
Array. An array of SetValue ((int). GetValue (i) * 2, I); Type conversions Required
}
}

private void Change (ArrayList ArrayList)
{
for (int i = 0; i < Arraylist.count; i++)//Use Count
{
Arraylist[i] = (int) arraylist[i] * 2; Type conversions Required
}
}
}


Other than that.

The Arraylist.adapter method can be used to convert an array of objects into ArrayList. This method wraps an IList in a ArrayList.

person[] Personarray = Myperson.getpersons ();
ArrayList personlist = Arraylist.adapter (Personarray);

You can use the Arraylist.toarray method to convert a ArrayList into an array of objects.

Person[] Personarrayfromlist = (person[]) Personlist.toarray (typeof (person));

Do not forget to force type conversion before calling the Arraylist.toarray method, or you will get an error when compiling it. You cannot convert a ArrayList into an array of person objects.

Analysis of the difference and application of [],list,array,arraylist] in C #

Related Article

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.