(200-07-07) C#.NET 集合(Collections)

摘要:(200-07-02) C#.NET 集合(Collections)

Mscorlib.dll

NameSpace System.Collections

陣列:1.元素集合 2.X Resize(固定長度) 3.型別語法

集合Class:1.操拃物件 2.動態參考( 初始空間 )3 .一律只參考 Object    4.分為有順序性與無順序二種

                    功能性操作 ICollections IList

參考列別

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
//集合應用-順序性
namespace csmod04
{
    class TestArrayList
    {
        //主程式
        public static void Main()
        {
            //1.建構集合物件
            ArrayList list = new ArrayList(3);
            //2.逐一參考物件
            list.Add("eric");
            list.Add("linda");
            list.Add(new Object());
            list.Add(new ArrayList());
            list.Add("bill");
            //3.參考幾個
            System.Console.WriteLine(list.Count);

            //順序性
            //次數迴圈(有順序及和順序從零開始)
            for (Int32 i = 0; i < list.Count; i++)
            {
                Object o = list[i]; //索引子語法list[]->this[順序]
                System.Console.WriteLine(o);
            }
            //逐一參考
            foreach (Object o in  list)
            {
                //特別針對字串類型
                if (o is String) //compare operator < > != 
                {
                    System.Console.WriteLine("字串:"+o.ToString());
                }
                if (o is ArrayList)
                {
                    System.Console.WriteLine("參考物件:" + ((ArrayList)o).Count.ToString());
                }
            }

        }
    }
}

值型別

 

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
//透過集合物件參考結構(?-結構沒有位址??)
namespace csmod04
{
    class TestColStruct
    {
        //主程式
        public static void Main()
        {
            //建構集合物件
            ArrayList list = new ArrayList();
            //參考結構
            list.Add(100); //autoboxing 將結構封裝成Object-才有位址
            list.Add(100.0);
            list.Add(1L);
            list.Add(100M);
            list.Add(new DateTime(2000, 1, 1));
            list.Add(true);

            //參考出來
            foreach (Object o in list)
            {
                if (o is Int32)
                {
                    System.Console.WriteLine(Int32.Parse(o.ToString())*100);
                }
                //不能將object 參考型別轉換成結構(值型別)
                System.Console.WriteLine(o); //UnBoxing 
            }
        }
    }
}