程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> List<>中Find的用法小結

List<>中Find的用法小結

編輯:C#入門知識

List<>中Find的用法小結。本站提示廣大學習愛好者:(List&lt;&gt;中Find的用法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是List&lt;&gt;中Find的用法小結正文


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

備注:在list和array聚集中搜刮元素常常應用該辦法,重要技巧是泛型拜托

list用法留意:假如增長一個對象,必需從新new一個對象,看上面的例子:

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

下面有差別嗎?在輸入list的值是有差別了,第一個list外面寄存的都是一樣的,成果和最初一個一樣,都是統一小我對象,第二個list到達預期的後果,存儲分歧的人對象。這是為何?緣由很簡略,一個list對象中存儲的都是對一個共有的對象停止援用,所以以最初轉變的值為准。

對象的排序:本文重要論述對存儲datatable的list停止按TableName排序

1、新建一個sort類

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 排序:
List<DataTable> ldt = new List<DataTable>();
SceneSort ss=new SceneSort ();
tablelist.sort(ss);便可對list停止排序;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved