ASP.NET Collections: Introduction
A database is collection of values. To create these values, you can use a collection class. Fortunately, instead of creating a class from scratch, the…
Introduction#
A collection of values forms a database. You can utilize a collection class to generate these values. Fortunately, the .NET Framework offers a robust set of collection classes and interfaces, saving you from building one from scratch. These built-in interfaces provide a base that other classes can use for implementing specific features. Different collection classes exist to serve various needs, with some available globally for Windows controls and others nested within specific classes. Support for these collections spans multiple namespaces in the .NET Framework, including System.Collections for standard types and System.Collections.Generic for their generic counterparts.
Accessories for Collections#
Reviewing stored values is one of the most common tasks performed on a database. To help with this, the .NET Framework includes the IEnumerator and IEnumerable interfaces within the System.Collections namespace. You can find their generic versions in the System.Collections.Generic namespace. Once these interfaces are implemented, the foreach operator can be used to iterate through database values. Implementing System.Collections.IEnumerator requires creating a derived class and defining the Reset() and MoveNext() methods, along with the Current property. Here is an example:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
/// <summary>
/// Summary description for Enumerator
/// </summary>
public class Enumerator : IEnumerator
{
private string[] names;
private int cur;
public Enumerator(string[] list)
{
this.names = list;
cur = -1;
}
public Object Current
{
get { return names[cur]; }
}
public void Reset()
{
cur = -1;
}
public bool MoveNext()
{
cur++;
if (cur < names.Length)
return true;
else
return false;
}
}To implement the System.Collections.IEnumerable interface, you must derive a class from it. When implementing the class, you must define an accessory method and the GetEnumerator() method that returns an IEnumerator object. Here is an example:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
/// <summary>
/// Summary description for Enumerable
/// </summary>
public class Enumerable : IEnumerable
{
private string[] names;
public Enumerable()
{
}
public void Identify(string[] values)
{
names = values;
for (int i = 0; i < values.Length; i++)
names[i] = values[i];
}
public IEnumerator GetEnumerator()
{
return new Enumerator(names);
}
}Once you have implemented the interfaces, you can use foreach. Here is an example:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="index.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>People Information</title>
</head>
<body>
<form id="frmPeople" runat="server">
<div>
<asp:ListBox ID="lbxNames" runat="server" Height="144px" Width="172px">
</asp:ListBox>
</div>
</form>
</body>
</html>using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var FullNames = new string[8];
FullNames[0] = "Gertrude Monay";
FullNames[1] = "Paul Bertrand Yamaguchi";
FullNames[2] = "Hermine Ngaleu";
FullNames[3] = "Francine Mukoko";
FullNames[4] = "Joseph Walters";
FullNames[5] = "Patricia Katts";
FullNames[6] = "Helen Cranston";
FullNames[7] = "Paul Motto";
var coll = new Enumerable();
coll.Identify(FullNames);
foreach (string s in coll)
lbxNames.Items.Add(s);
}
}Choosing an Interface#
Although the IEnumerator and IEnumerable interfaces are essential for enabling enumeration in a collection class, other interfaces are also available to help you build the specific functionality your collection needs. Before creating a custom class, it is wise to check the existing options provided by the .NET Framework. If no built-in class meets your requirements, you can develop your own by implementing one or more interfaces. The .NET Framework includes several widely used interfaces, such as:
- System.Collections.IComparer and System.Collections.Generic.IComparer : If you derive a class from this interface, you can define how two objects would be compared for similarity or difference
- System.Collections.IDictionary and System.Collections.Generic.IDictionary : This interface is used to create a collection class where each item is made of a key=value combination
Home Copyright © 2009 C# Key Next

