C#汎用集合-List<br>テクニックの使用


    List<T>
                ,               ,          。
  List<T>    ArrayList  ,List<T>         ,   ,  。    List<T>         :

List<T>    =new List<T>();

        ,      “<T>”      ,  T           ,        ,          。    T         。    List<T>          ,“<T>”  T               。

  :         ,              ,       “()”。

           List<T>,    :

List<Student> students=new List<Student>();
  List<T>  、  、        ArrayList  ,          ArrayList       。

using System;
using System.Collections.Generic;
using System.Collections;
public class Student
{
public string Name
{
   get;
   set;
}
public string Number
{
   get;
   set;
}
public int Score
{
   get;
   set;
}
}
class Program
{
static void Main()
{
   List < Student > students = new List < Student > ();
   Student stu1 = new Student();
   stu1.Name = "   ";
   stu1.Number = "0801";
   stu1.Score = 20;
   Student stu2 = new Student();
   stu2.Name = "    ";
   stu2.Number = "0802";
   stu2.Score = 23;
   students.Add(stu1);
   students.Add(stu2);
   Console.WriteLine("         {0}", students.Count);
   foreach (Student stu in students)
   {
    Console.WriteLine("\t{0}\t{1}\t{2}", stu.Name, stu.Number, stu.Score);
   }
   students.Remove(stu1);
   Console.WriteLine("         {0}", students.Count);
   Console.ReadLine();
}
}

         Student   List<T>    ,   T     Student  ,           Student     ,       ,       ,        ,     。

List<T> ArrayList   

  List<T> ArrayList    :    、    、            。
  List<T> ArrayList    :
ArrayList          ;List<T>            ;
ArratList     ,     ;List<T>     ,    ;

        :

using System;
using System.Collections;
using System.Collections.Generic;
class Person
{
private string _name; //  
private int _age; //  
//  Person  
public Person(string Name, int Age)
{
   this._name = Name;
   this._age = Age;
}
//  
public string Name
{
   get
   {
    return _name;
   }
}
//  
public int Age
{
   get
   {
    return _age;
   }
}
}
class Program
{
static void Main()
{
   //  Person  
   Person p1 = new Person("  ", 30);
   Person p2 = new Person("  ", 20);
   Person p3 = new Person("  ", 50);
   //     Person     
   List < Person > persons = new List < Person > ();
   // Person      
   persons.Add(p1);
   persons.Add(p2);
   persons.Add(p3);
   //   2     
   Console.WriteLine(persons[1].Name);
   foreach (Person p in persons)
   {
    Console.WriteLine("\t{0}\t{1}", p.Name, p.Age);
   }
}
}

      ,                ,   ,             。    ,               ,            。

           COMBOX       

    public class vendor
    {
        private string _UserCode;
        private string _VendorDes;

        public string VendorDes
        {
            get { return _VendorDes; }
            set { _VendorDes = value; }
        }
        public string UserCode
        {
            get { return _UserCode; }
            set { _UserCode = value; }
        }
        public vendor(string display, string value)
        {
            _VendorDes = display;
            _UserCode = value;
        }
        public vendor() { }
    }
        /// <summary>
        ///        
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public  List<Model.vendor>GetVendList()
        {
            List<Model.vendor> lstV = new List<Model.vendor>();
            string sql = string.Format("select code,vendordes from vendor");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.conn, CommandType.Text, sql))
            {
              
                while (dr.Read())
                {
   
                    lstV.Add(new Model.vendor(dr["code"].ToString().Trim (), dr["vendordes"].ToString().Trim ()));
                  
                }
              
            }
            return lstV;
         
        }


           //  COMBOX
            BLL.PO vd = new BLL.PO();
            List<Model.vendor> lst =new List<Model.vendor>();
            lst =vd.GetVendList();

            foreach (Model.vendor v in lst)
            {
                string code = v.UserCode;
                string vendor = v.VendorDes;
         
                comboBox1.Items.Add(vendor);
                comboBox1.DisplayMember = code;
                comboBox1.ValueMember = vendor;
            }

テキストリンク:http://www.code-design.cn/article/20120210/csharp-generic-list-object.aspx