Generic List IndexOf() Method
.Net framework generic list IndexOf(T) method search for the specified object and return the zero based index of the first occurrence
within the entire List<T>. the IndexOf() method exists in System.Collections.Generic namespace. this IndexOf(T) method require to pass a parameter
name 'item'. the 'item' parameter type is T. this 'T' represents the object to locate in the List<T> and the value can be null for reference types.
the method return value data type is System.Int32. return value is a zero based index of the first occurrence of 'item' within the entire List<T>, if the item found. otherwise the method return -1. the method implements as IList<T>.IndexOf(T). if the list contains multiple elements of same name then it return the index of first occurrence.
the following asp.net c# example code demonstrate us how can we get the index number of specific elements in a generic list in an asp.net application.
the method return value data type is System.Int32. return value is a zero based index of the first occurrence of 'item' within the entire List<T>, if the item found. otherwise the method return -1. the method implements as IList<T>.IndexOf(T). if the list contains multiple elements of same name then it return the index of first occurrence.
the following asp.net c# example code demonstrate us how can we get the index number of specific elements in a generic list in an asp.net application.
GenericListIndexOfMethod.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
List<string> colors = new List<string>() {"SaddleBrown","OldLace","DarkBlue"};
colors.Add("FloralWhite");
colors.Add("PeachPuff");
colors.Add("DarkBlue");
Label1.Text = "List Elements....<br />";
foreach (string color in colors)
{
Label1.Text += "<br />" + color;
}
Label1.Text += "<br /><br />IndexOf color 'DarkBlue': " + colors.IndexOf("DarkBlue");
Label1.Text += "<br /><br />IndexOf color 'PeachPuff': " + colors.IndexOf("PeachPuff");
Label1.Text += "<br /><br />IndexOf color 'Red': " + colors.IndexOf("Red");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Generic List IndexOf() - How to get the specific List element's index</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkOrchid; font-style:italic;">
System.Collections.Generic.List IndexOf() Method
<br /> How to get the specific List element's index
</h2>
<hr width="525" align="left" color="Purple" />
<asp:Label
ID="Label1"
runat="server"
ForeColor="SlateBlue"
Font-Size="Large"
Font-Names="Courier New"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Test Generic List IndexOf() Method"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>

- Generic List GetRange() method
- Initializing Generic List
- Initialize an ArrayList
- Generic List ToArray() method
- ArrayList CopyTo() method with starting index
- Generic List LastIndexOf() method
- Generic List RemoveAt() method
- ArrayList Remove() method
- Generic List Reverse() method
- Generic List Reverse() method with range