Find usage in list

Source: Internet
Author: User

I 've been looking for help on how to find
Objects in generics with list. Find () method... and... take a look
What I have found.
In the follow example, I created a simple class:

Public class person
{
Private int _ id;
Private string _ name;

Public int ID {get {return _ id;} set {_ id = value ;}}
Public int name {get {return _ name;} set {_ name = value ;}}

Public Person (int id, string name)
{
_ Id = ID;
_ Name = Name;
}
}

In
The example, there's a simple class with two private attributes. Now
We're re going to create a typed list of this object and take advantage
The find () method

Public void createandsearchlist ()
{
// Create and fill the collection
List <person> mylist = new list <person> ();
Mylist. Add (new person (1, "andreysanches "));
Mylist. Add (new person (2, "alexandretarifa "));
Mylist. Add (new person (3, "emersonfacunte "));

// Find a specific object
Person mylocatedobject = mylist. Find (delegate (person p) {return P. ID = 1 ;});
}

Note: This method is often used for searching elements in list and array sets. The main technology is generic delegation.

Note: if an object is added, a new object must be added. See the following example:

Person P = new pewson ();
For (INT I = 0; I <5; I ++)
{
P. ID = I;
P. Name = "XXXX ";
List. Add (P );
}

For (INT I = 0; I <5; I ++)
{
Person P = new person ();
P. ID = I;
P. Name = "XXXX ";
List. Add (P );
}

Upper
Is there a difference between the two sides? The value of the output list is different. The first list stores the same result and the last one is the same person object. The second list achieves the expected effect.
Result: different objects are stored. Why? The reason is simple. All objects stored in a list object are referenced by a common object, so the last changed value prevails.

Object sorting: This article mainly describes how to sort the list storing datatable by tablename.

1. Create a New Sort class

Public class scenesort: icomparer <datatable>
{
Public int compare (datatable obj1, datatable obj2)
{
Int tablenamelength1;
Int tablenameleng2;
If (obj1 = NULL)
{
If (obj2 = NULL)
Return 0;
Else
Return-1;
}
Else
{
If (obj2 = NULL)
Return 1;
Else
{
Tablenamelength1 = obj1.tablename. length;
Tablenamelength1 = obj2.tablename. length;

If
(Convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2)> convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2 )))

Return 1; // return 1 if the value is greater

Else if
(Convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2) <convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2 )))

Return-1; // less than-1
Else
Return 0; // return 0 if they are equal
}
}

}
2 sorting:
List <datatable> LDT = new list <datatable> ();
Scenesort Ss = new scenesort ();
Tablelist. Sort (SS); to sort the list;

I 've been looking for help on how to find
Objects in generics with list. Find () method... and... take a look
What I have found.
In the follow example, I created a simple class:

Public class person
{
Private int _ id;
Private string _ name;

Public int ID {get {return _ id;} set {_ id = value ;}}
Public int name {get {return _ name;} set {_ name = value ;}}

Public Person (int id, string name)
{
_ Id = ID;
_ Name = Name;
}
}

In
The example, there's a simple class with two private attributes. Now
We're re going to create a typed list of this object and take advantage
The find () method

Public void createandsearchlist ()
{
// Create and fill the collection
List <person> mylist = new list <person> ();
Mylist. Add (new person (1, "andreysanches "));
Mylist. Add (new person (2, "alexandretarifa "));
Mylist. Add (new person (3, "emersonfacunte "));

// Find a specific object
Person mylocatedobject = mylist. Find (delegate (person p) {return P. ID = 1 ;});
}

Note: This method is often used for searching elements in list and array sets. The main technology is generic delegation.

Note: if an object is added, a new object must be added. See the following example:

Person P = new pewson ();
For (INT I = 0; I <5; I ++)
{
P. ID = I;
P. Name = "XXXX ";
List. Add (P );
}

For (INT I = 0; I <5; I ++)
{
Person P = new person ();
P. ID = I;
P. Name = "XXXX ";
List. Add (P );
}

Upper
Is there a difference between the two sides? The value of the output list is different. The first list stores the same result and the last one is the same person object. The second list achieves the expected effect.
Result: different objects are stored. Why? The reason is simple. All objects stored in a list object are referenced by a common object, so the last changed value prevails.

Object sorting: This article mainly describes how to sort the list storing datatable by tablename.

1. Create a New Sort class

Public class scenesort: icomparer <datatable>
{
Public int compare (datatable obj1, datatable obj2)
{
Int tablenamelength1;
Int tablenameleng2;
If (obj1 = NULL)
{
If (obj2 = NULL)
Return 0;
Else
Return-1;
}
Else
{
If (obj2 = NULL)
Return 1;
Else
{
Tablenamelength1 = obj1.tablename. length;
Tablenamelength1 = obj2.tablename. length;

If
(Convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2)> convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2 )))

Return 1; // return 1 if the value is greater

Else if
(Convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2) <convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2 )))

Return-1; // less than-1
Else
Return 0; // return 0 if they are equal
}
}

}
2 sorting:
List <datatable> LDT = new list <datatable> ();
Scenesort Ss = new scenesort ();
Tablelist. Sort (SS); to sort the list;

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.