Introduction to the Generic List - Part I
August 28th, 2007
Earlier I had gone over some other aspects of the System.Collections.Generic namespace, such as the Queues, Stacks, and Action and Predicate Delegates. I received a couple of emails asking me to also go over the plain old List object as well and include any helpful tricks I might have for them. So, this will be the topic for this two part series of articles.
The Basics
The generic List is a type-safe version of the .NET ArrayList. It can dynamically change its size as items are added or removed. It can also be searched, sorted and enumerated. Since the List is type-safe, meaning you have to specify the type of items going into it, it will generally perform better than ArrayList. All in all, the generic List is a better way to store data in memory in most cases than the older ArrayList and plain old array models.
Adding and Removing Items
While The Add, Remove, and Clear methods are obvious, there are some additional related methods that you might find useful.
We’ll look at AddRange first. As you can guess, it loads more than one item at once to the List. It might be a bit daunting for you when you look at the parameter required for this method.
collection As System.Collections.Generic.IEnumerable(Of T)
What does that mean? What it is looking for here is a collection that implements the IEnumerable interface and has the right type. This includes standard arrays and many other collection types just as with several other objects that implement this method in the .NET Framework. Let’s look at a few examples.
In this example, we’re going to take a comma delimited string entered in a TextBox and place it in our List:
Dim EmailAddresses As New List(Of String)
EmailAddresses.AddRange(txtEmailAddresses.Text.Split(","c))
In this example, we’re going to load the selected items from a ListBox:
Dim EmailAddresses As New List(Of String) Dim AddressArray(lstEmails.SelectedItems.Count - 1) As String lstEmails.SelectedItems.CopyTo(AddressArray, 0) EmailAddresses.AddRange(AddressArray)
This routines involves a little trickery because we need a temporary string array to convert the Object types in the SelectedItems collection via the CopyTo method.
In addition to simply adding onto a list we can use the InsertRange method to place items after a particular location in the List, including the beginning, as seen here:
EmailAddresses.InsertRange(0, txtEmailAddresses.Text.Split(","c))
When we want to remove more than one item, we can use the RemoveRange method, where we can specify the starting position and the number to remove:
EmailAddresses.RemoveRange(3, 2)
If we want to remove items that match a particular criteria, we can use the RemoveAll method. This method uses a Predicate delegate:
EmailAddresses.RemoveAll(AddressOf ValidateEmailAddress)
...........
Public Function ValidateEmailAddress(ByVal emailAddress As String) As Boolean
Dim pattern As String = "[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*.([a-z][a-z|0-9]*(.[a-z][a-z|0-9]*)?)$"
Dim match As System.Text.RegularExpressions.Match = Regex.Match(emailAddress, pattern, RegexOptions.IgnoreCase)
Return match.Success
End Function
(Note for the validate email function to work you’ll need to import System.Text.RegularExpressions)
Sorting
To sort the List we can use default comparison method or we can define our own sorting routine as a Delegate or as class that implements the IComparer interface. For the sake of brevity, I won’t include the sorting code here, but only give an example of how it’s called. If you would like an article with more detail on these sorting methods, please leave me a comment or contact me. Here are some example calls:
EmailAddresses.Sort(AddressOf SortEmails) ....... Dim EmailSorter As New EmailComparer EmailAddresses.Sort(EmailSorter)
Along the same lines as sorting is the Reverse method. This method, as you might guess, simply reverses the order in the list. You can also specify a starting position and length to reverse.
That’s is all for Part I. Part II will cover searching, conversions and enumeration functions available in the List object. As always, let me know if you found this overview article helpful by leaving a comment.
Entry Filed under: VB.NET Tutorials
Rate This Article:










1 Comment Add your own
1. ajay | September 25th, 2008 at 7:22 am
how to use generic colletion shorting in vb.net
Can u provide that code
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed