Tuesday, September 6, 2011

Using ASP.NET 2.0-List Class


Using ASP.NET 2.0-List Class in your code

You can use the System.Collections.Generic.List class to embody a strongly typed list of objects that may be accessed by index. The class provides methods to search, sort, and manipulate the contents of the list. The Microsoft documentation describes the List class as the generic equivalent of the ArrayList class.

Methods of list Class:

Add: Allows you to add an object to the end of the List.


AddRange: Adds the contents of one collection to the end of the List.


BinarySearch: Uses a binary search algorithm to locate a specific element in the sorted List or a portion of it.


Clear: Clears the contents of the List (i.e., everything is deleted).


Contains: Determines if the List contains a specific element.


ConvertAll: Converts all List elements to another type.


CopyTo: Copies the contents of the List to an array.


Exists: Determines if the List contains element(s) matching a condition.


Find: Searches for an element that matches certain conditions.


FindAll: Locates all elements that match certain conditions.


FindIndex: Locates index of element that matches a condition.


FindLast: Searches for the last occurrence of an element that matches a certain condition.


FindLastIndex: Searches for the last occurrence of an element that matches a certain condition and returns its index.


ForEach: Performs a specific action on each List item.


GetEnumerator: Returns an enumerator that iterates through the List.


GetRange: Returns a subset of elements from the List.


IndexOf: Returns the first index of element that matches a value.


LastIndexOf: Returns the last index of element that matches a value.


Remove: Removes the first occurrence of a specific element from the List.


RemoveAll: Removes all elements from the List that match the conditions.


RemoveAt: Removes element from the List with specific index.


RemoveRange: Removes group of elements from the List.


Reverse: The order of List elements is reversed.


Sort: Sorts the order of List elements by key value.


Following example example use customized ASP.NET 2.0 List Class

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;




using System.Collections.Generic;

using System.Text;

public partial class cGenerics : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{


List<Person> objPerson = new List<Person>();
objPerson.Add(new Person("First0 Last0"));objPerson.Add(
new Person("First1 Last1"));
objPerson.Add(new Person("First2 Last2", 24, "XXZ Street"));objPerson.Add(
new Person("First3 Last3", 25, "avc Street"));


//Remove this person from list
objPerson.RemoveAll(delegate(Person person){ return person.Name == "First1 Last1"; });



foreach (Person p in objPerson)
{



Response.Write(p.Name);
Response.Write(
"&nbsp;");
Response.Write(p.Age);
Response.Write(
"&nbsp;");
Response.Write(p.Address);
Response.Write(
"&nbsp;");


Response.Write(p.Company);
Response.Write(
"<br>");
}

}



}

class Person

{
int _Age;public int Age
{

get { return _Age; }
set { _Age = value; }
}
String _Name;public String Name
{

get { return _Name; }
set { _Name = value; }
}
String _Address;public String Address
{

get { return _Address; }
set { _Address = value; }
}
String _Company;public String Company
{

get { return _Company; }
set { _Company = value; }
}
public Person() { }public Person(String Name)
{

this.Name = Name;
this.Age = 0;
this.Address = String.Empty;
this.Company = String.Empty;
}
public Person(String Name, int Age, String Address)
{
this.Name = Name;
this.Age = Age;
this.Address = Address;
}

}

No comments:

Post a Comment

Open default email app in .NET MAUI

Sample Code:  if (Email.Default.IsComposeSupported) {     string subject = "Hello!";     string body = "Excellent!";    ...