This Tutorial is second part of my previous Generic List Tutorial, In this tutorial I will cover some more features which are left in previous one. Generic List has some more attractive features like Find Item, Insert Item etc.
This article will cover the following topics:

Figure 1: List Items with Social Network Data
ASP.NET is used here to display the List data and interact with the user. ASP.NET TextBox Controls, ASP.NET Label Controls, ASP.NET Buttons Controls and ListBox Control are added in ASP.NET page.
Listing 1: ASP.NET Script for List Demonstration
<head runat="server">
<title>MrBool.com - List Tutorial Part 2</title>
</head>
<body>
<form id="form1" runat="server">
<div>
List Item:
<asp:TextBox ID="txtListItem" runat="server"></asp:TextBox>
<br />
<br />
Message:
<asp:Label ID="lblListDisplay" runat="server" Text=""></asp:Label>
<br />
<br />
List State:<br />
<asp:ListBox runat="server" ID="lstSocialNetwork" Width="200px" Height="150px"
> </asp:ListBox>
<br />
<br />
<asp:Button ID="btnInsert" runat="server" Text="Insert"
onclick="btnInsert_Click" />
<asp:Button ID="btnListtoArray" runat="server" Text="List to Array"
onclick="btnListtoArray_Click" />
<asp:Button ID="btnGetRange" runat="server" Text="Get Range" onclick="btnGetRange_Click"
/>
<asp:Button ID="btnFind" runat="server" Text="Find" onclick="btnFind_Click"
/>
</div>
</form>
</body>
Starting with C# code, First add the namespaces required for the List collection. Because list is a generic collection so add the following namespaces:
Listing 2: NameSpaces for List Collection
using System; using System.Collections.Generic;
Now you have to create the List collection object and initialize it like below code.
Listing 3: List Object Declaration
static List<string> SocialNetworkList; SocialNetworkList = new List<string>();
Now you have a List object of type string. Note that because this is string type, so you can also insert the int and other types(The types whose conversion is supported by string) into it but it will increase the conversion cost (Remember the concept of boxing and unboxing).
Similar to Add() method List provide a Insert() method to insert an item at a given location. Add() method add an item at the end only while using Insert() you can add an item at any given position. In given example it is inserting the “Digg” item at position “2” in the List. Note that List indexing start from “0”.

Figure 2: Insert Item - An item Inserted (“Digg”)
Listing 4: List- Insert Item Source Code
protected void btnInsert_Click(object sender, EventArgs e)
{
if (txtListItem.Text.Trim() != "")
{
SocialNetworkList.Insert(2,txtListItem.Text.Trim());
lblListDisplay.Text = "List Item Inserted at 2nd position";
}
else
{
lblListDisplay.Text = "List - Please Enter an Item to Insert";
}
DisplayData();
}
Similar to some other collection classes List provide a method CopyTo() to copy the items into a given array . The below example explain the concept. First Define an array and then use CopyTo() method to copy items into Array. Note that the Array should be big enough to hold all the items getting copied.

Figure 3: List is Converted in Array and Printed on Screen
Listing 5: List to Array Conversion- Code
protected void btnListtoArray_Click(object sender, EventArgs e)
{
string[] strArray = new string[SocialNetworkList.Count];
SocialNetworkList.CopyTo(strArray);
foreach (string str in strArray)
{
lblListDisplay.Text += "," + str;
}
lblListDisplay.Text = lblListDisplay.Text.TrimStart(',');
}
List provide a GetRange() method to get the sublist from a given List. You can specify the Initial position and then length of the sublist and method will return the whole sublist. For example in the below program the GetRange method is called with (2,2) argument, it means start from 2 index and return the sublist of 2 length.

Figure 4: Get Range Items - The range (2,2) is fetched from List
Listing 6: List- Get Range Items- Code
protected void btnGetRange_Click(object sender, EventArgs e)
{
string[] strArray = new string[100];
List<string> tempNetworkList = SocialNetworkList.GetRange(2,2);
foreach (string str in tempNetworkList)
{
lblListDisplay.Text += "," + str;
}
lblListDisplay.Text = lblListDisplay.Text.TrimStart(',');
}
Find() method is used here to find an item in List. The Find() method support the Linq expression to pass the search query.
For Example the below code is using “item => item == txtListItem.Text.Trim()” it means search list with item name = “Given item”.
In below example, It is searching for “Google+” and it found in List.

Figure 5: Find “Google+” in List
Listing 7: List- Find data - Code
protected void btnFind_Click(object sender, EventArgs e)
{
if (txtListItem.Text.Trim() != "")
{
string str = SocialNetworkList.Find(item => item == txtListItem.Text.Trim());
if (str != "")
{
lblListDisplay.Text = "List Item Found";
}
else
{
lblListDisplay.Text = "List Item Not Found.";
}
}
else
{
lblListDisplay.Text = "Please Enter an Item to Search";
}
}
Apart from above common operations, C# List provide some more common functions.
BinarySearch: List provide binary search on List items and return the index.
IsExist: This method returns true or false for an search
IndexOf: The method returns the index of first occurrence of a given item
LastIndexOf: The method returns the index of last occurrence of a given item
Listing 8: List- Other operation
private void OtherFunctionality()
{
//BinarySearch
int Index = SocialNetworkList.BinarySearch(txtListItem.Text.Trim());
//Exists
bool IsExist = SocialNetworkList.Exists(item => item == "Twitter");
lblListDisplay.Text = " Get the index of Item: " + SocialNetworkList.IndexOf(txtListItem.Text.Trim());
lblListDisplay.Text = " Get the LastIndexOf of item: " + SocialNetworkList.LastIndexOf(txtListItem.Text.Trim());
}
Now I have done with the List Tutorial Part1 and Part2. The tutorials covered the good amount of features and operations of List. Go through both the tutorials and Comment here if you have any questions.








See the prices for this post in Mr.Bool Credits System below: