C # array differs from ArrayList and gets SQL field name

Source: Internet
Author: User

The difference between array and ArrayList:

1, the capacity of the Array is fixed, and the capacity of the ArrayList is automatically expanded as needed. If you change the value of the Arraylist.capacity property, memory reallocation and element replication are performed automatically.

2. ArrayList provides a way to add, insert, or remove a range of elements. In an Array, you can only get or set the value of one element at a time.

3. Properties in ArrayList

The Count property is the number of elements currently contained in the ArrayList, and this property is read-only.

The capacity property is the maximum number that can be contained by the current ArrayList, which is set manually, but throws an exception when set to less than the count value.

Isfixedsize gets a value that shows whether it has a fixed size.

Item Gets or sets the element of the specified index

4. Common method attributes in ArrayList

The Add method adds an element to the end of the current list

The AddRange method is used to add a batch of elements to the end of the current list

The Remove method is used to delete an element that is removed by a reference to the element itself

The RemoveAt method is used to delete an element by using the index value to remove

RemoveRange is used to delete a batch of elements by specifying the starting index and the number of deletions to delete

Inserts are used to add an element to the specified position, and the elements following the list move backward in turn

Insertrange is used to add a batch of elements starting at the specified position, and the elements behind the list move backwards

CopyTo copy a ArrayList or part of it into a one-dimensional array

ToArray copy elements of ArrayList into a new array

TrimToSize set capacity to the actual number of elements in ArrayList

Clear remove all elements from the ArrayList

5. Convert ArrayList to array

ToArray Method:

ArrayList al=new ArrayList ();

Int[] ss= (int[]) al. ToArray ();

CopyTo Method:

ArrayList al=new ArrayList ();

Int[] Ss=new Int[al.count];

Al. CopyTo (ss);

Difference: (reprint)

ArrayList is a dynamic array that does not include algorithms that are accessed quickly through key or value, so actually calling IndexOf, contains, and so on is a simple loop of execution to find elements, so frequent calls to such methods are no faster than writing loops and optimizations, If this is a requirement, it is recommended to use a collection of key-value pairs such as Hashtable or SortedList.

ArrayList provides methods for returning a read-only and fixed-size wrapper back to the collection. And Array is not available.

On the other hand, Array provides some flexibility that ArrayList does not have. For example:

The lower bound of the Array can be set, but the lower bound of the ArrayList is always zero.

An Array can have multiple dimensions, and ArrayList is always just one-dimensional.

An Array of a particular type (excluding Object) has better performance than ArrayList because the elements of ArrayList are of type Object, so boxing and unboxing typically occur when a value type is stored or retrieved.

Most cases of requiring an array can also be replaced with ArrayList. It is easier to use and typically has performance similar to an array of type Object.

The Array is in the System namespace; ArrayList is in the System.Collections namespace.

Get the field name in sql:

Dr = Sqlcmd.executereader ();
for (int i=0;i<=dr. fieldcount-1;i++)
{
S+=dr. GetName (i);

List. ADD (Dr. Getnae (i));
}

C # array differs from ArrayList and gets SQL field name

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.