Collections and lists (arrays) in ASP. net2.0)

Source: Internet
Author: User

I would like to talk about the collection application in ASP. net2.0.

1. Arrays

Array is the simplest collection of objects. Let's start with a class of person.

The concept of classes is not mentioned. Open a project and create a new app_code folder (where class files are placed.

Click the app_code folder to add a class file "person. cs.

The person. CS code is as follows:

Using system;
Using system. Data;
Using system. configuration;
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;

/// <Summary>
/// Summary description for person
/// </Summary>
Public class person
{
String firstname;
String lastname;
Public Person (string first, string last)
{
Firstname = first;
Lastname = last;
//
// Todo: Add constructor logic her //
}

Public String fullname
{
Get
{
Return firstname + "" + lastname;
}
}
}

Create a new showperson. aspx file with the Post code showperson. aspx. CS

Write page_load in showperson. aspx. CS as follows:

Protected void page_load (Object sender, eventargs E)
{
Person Jimmy = new person ("Jimmy", "Zeng ");
Person stone = new person ("Stone", "Wang ");
Person Helen = new person ("Helen", "Liu ");

Person [] People = {Jimmy, stone, Helen };
Response. Write ("We have output the name now <br/> ");
Foreach (person P in People)
{
Response. Write (P. fullname + "<br/> ");
}
}

This means that person generates three name objects, namely Jimmy/stone/Helen. Then an array people.

Finally, use foreach to obtain the fullname of each object. You can see the result after you run it.

Adjust arrays size

The only way to adjust the size of arrays in C # is to copy the previous array to a large array.

Person [] arypeople2 = new person [5]

Array. Copy (people, arypeople2, people. Length ):

Search for objects in Arrays
Array is the simplest type of collection. We usually use index values to quickly locate (index ).
Of course, when searching for an object, we need to find out whether we want to find an object with the same attribute or whether we want to find an object with the same value as the target object. there are two fundamental differences.
OK
Looking for an object in an array by reference
Person [] People = {Jimmy, stone, Helen };
Int indexofjimmy = array. indexof (people, Jimmy );
Response. Write ("Jimmy is at" + indexofjimmy + "<br/> ");
At this time, the output is
Jimmy is at 0

Another example
Person Jimmy = new person ("Jimmy", "Zeng ");
Person stone = new person ("Stone", "Wang ");
Person Helen = new person ("Helen", "Liu ");
Person [] People = {Jimmy, stone, Helen };
Person jimmy2 = new person ("Jimmy", "Zeng ");
Int indexofjimmy2 = array. indexof (people, jimmy2 );
Response. Write ("jimmy2 is at" + indexofjimmy2 + "<br/> ");
The running result is: jimmy2 is at-1.
Of course, it indicates that jimmy2 is no longer in the array of people. Of course, our intention is not to search for the jimmy2 object, but to search for objects with the same attributes as jimmy2. What should we do /? /?

Let's talk about the comparison between the two objects.
First, use object. equals to override.
Open the person. Cs in the app_code directory and reload the following code.

Public override bool equals (Object OBJ)
{
Person Other = OBJ as person;
Return (other. lastname = This. lastname & other. firstname = This. firstname );
}
Then we make a comparison in showpeople. aspx. CS.
Person jimmy2 = new person ("Jimmy", "Zeng ");
If (jimmy2.equals (Helen) = true)
{
Response. Write ("OK ");
}
Else
{
Response. Write ("false ");
}
}
The output is false. OK is used to compare objects.
Import icomparable
To facilitate sorting by array, we import icomparable
The import method is to modifyThe person. CS code is as follows:

Using system;
Using system. Data;
Using system. configuration;
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;

/// <Summary>
/// Summary description for person
/// </Summary>
Public class person: icomparable
{
String firstname;
String lastname;
Public Person (string first, string last)
{
Firstname = first;
Lastname = last;
//
// Todo: Add constructor logic her //
}

Public String fullname
{
Get
{
Return firstname + "" + lastname;
}
}
}
OK. It has been implanted. It is used.
First, use a sort statement.
Array. Sort (people)
Then we will look for an equal array. Here we use array. binarysearch.
Int indexofjimm2 = array. binarysearch (people. jimmy2 );
In this way, the location of Jimmy in the array with the Same bucket jimmy2 is returned.

Now there is a more powerful arraylist in ASP. net2.0.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.