List usage in C #

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; _ Nam E = 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 of the find () methodpublic 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, "E Mersonfacunte "); // find a specific object person mylocatedobject = mylist. find (delegate (person p) {return p. id = 1 ;}) ;}note: This method is often used by search elements in the list and array sets. The main technology is the usage of generic delegated list. Note: if an object is added, A new object must be created. For 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);} is there any difference? The value of the output list is different. The first list stores the same results and the last one is the same person object. The second list achieves the expected results, stores different people objects. 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.
; "Id =" _ mcepaste "> 1. Create an 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; // returns 1 else if (convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2) <convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2) Return-1; // less than returned-1 else return 0; // equal return 0 }}2 sort: 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:

 

Code

 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
Going to create a typed list of this object and take advantage of the find ()
Method

Code

 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);
}

 

Is there any difference? The value of the output list is different. The first list stores the same results and the last one is the same person object. The second list achieves the expected results, stores different people objects. 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

 

Code

 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;

 

 

 

 

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/cxb_wind/archive/2008/04/10/2277670.aspx

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.