List of Find usage

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 going to create a typed List of this object and take advantage of 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: In the list, and search the collection array element often used this method, the main technology is generic delegate

list Usage Note: If you add an object, you must re-new object, 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 a difference above? The value of the output list there is a difference, the first list stored inside are the same, the results and the last, are the same person objects, the second list to achieve the desired effect, people of different storage objects. Why is that? The reason is simple, a list of the objects are stored in a shared object references, so in order to change the value of the last subject.

Sort objects: This article focuses on the list of storage datatable be sorted by TableName

1, a new sort class

public class SceneSort:IComparer<DataTable>
    {
       public int Compare(DataTable obj1, DataTable obj2)
       {
           int tableNameLength1;
           int tableNameLength2;
           if (obj1 == null)
           {
               if (obj2 == null)
                   return 0;
               else
                   return -1;
           }
           else
           {
               if (obj2 == null)
                   return 1;
               else
               {
                   tableNameLength1=obj1.TableName.Length;
                   tableNameLength2=obj2.TableName.Length;
                   if (Convert.ToInt32(obj1.TableName.Substring(2,tableNameLength1-2))>Convert.ToInt32(obj2.TableName.Substring(2,tableNameLength2-2)))
                       return 1;                            //大于返回1
                   else if (Convert.ToInt32(obj1.TableName.Substring(2,tableNameLength1-2))<Convert.ToInt32(obj2.TableName.Substring(2,tableNameLength2-2)))
                       return -1;                           //小于返回-1
                   else
                       return 0;                            //相等返回0
               }
           }

       }
2 Sort:
List <the DataTable> = LDT new new List <the DataTable> ();
SceneSort new new SceneSort SS = ();
tablelist.sort (SS); of the list to be sorted;

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/05/18/2050353.html

Guess you like

Origin blog.csdn.net/weixin_34245749/article/details/93496105