[ad_1]
ArrayList is a really helpful information construction in C #. Although since .NET framework 2.0, programmers have a better option (Generic List) to assist them in coping with a listing of objects, ArrayList continues to be fairly helpful as a result of it helps some performance in some instances which Generic List doesn't assist, in addition to it has higher efficiency in some events; Therefore, I'll publish a sequence examples and tricks to present use ArrayList in C #, particularly for C # newcomers and programmers who should not acquainted with ArrayList.
Today our subject is utilizing Add () methodology so as to add new merchandise to an ArrayList. One large ArrayList enhancement from conventional arrays is that ArrayList is dynamic, which signifies that programmers can change its dimension in run time after its initialization by calling strategies corresponding to Add () and Delete (). When Add () is named, a brand new merchandise might be appended to the very finish of the ArrayList. Of course, we it's allowed to insert a merchandise to the particular place in a ArrayList by calling Insert () methodology, however now let's give attention to the Add (). The syntax of Add () could be very easy:
public digital int Add (Object merchandise)
Here parameter merchandise is the merchandise to be added to the tip of the ArrayList. Since its information kind is Object, programmers can add any information kind of merchandise into ArrayList corresponding to beneath code:
ArrayList myAL = new ArrayList ();
myAL.Add ("a string");
myAL.Add (1); // no exception, however not good follow
myAL.Add (new object ());
myAL.Add (DataTime.Now); // no exception, however not good follow
But one factor we'd like take into accout, do NOT add Value kind to an ArrayList, as a result of it will possibly trigger Boxing and Unboxing operations which has large efficiency difficulty in C # .Net; due to this fact, above code are all appropriate (no exception), however it isn't good follow in utilizing ArrayList. Programmers ought to at all times solely add merchandise kind kind is Reference Type Not Value Type. Below let's see a whole instance code which present the utilization of Add () methodology.
// The code is simplified from MSDN,
// and has been examined on vs2010 with.Net Framework 4
utilizing System;
utilizing System.Collections;
public class SamplesArrayList
{public static void Main ()
{// Creates and initializes a brand new ArrayList.
ArrayList exampleArrayList = new ArrayList ();
exampleArrayList.Add ("The");
exampleArrayList.Add ("quick");
exampleArrayList.Add ("brown");
exampleArrayList.Add ("fox");// Displays the ArrayList and the Queue.
Console.WriteLine ("The ArrayList initially contains the following:");
PrintValues (exampleArrayList, ' t');
}public static void PrintValues (IEnumerable myList, char mySeparator)
{
foreach (Object obj in myList)
Console.Write ("{0} {1}", mySeparator, obj);
Console.WriteLine ();
}
}/ *
This code produces the next output.The ArrayList initially accommodates the next:
The fast brown fox
* /
Now it's the time to attempt the code by your self.
[ad_2]