Sunday, March 22, 2009

System.Collections in C#.NET

ที่มา: http://www.codetoday.net/Default.aspx?g=posts&t=328

# คลาสต่างๆที่อยู่ใน System.Collections เช่น ArrayList , Hashtable ,Stack,Queue,SortedList

การใช้งาน ArrayList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication17
{
class Program
{
static int Main(string[] args)
{
ArrayList arr = new ArrayList();
arr.Add(new Person("Jane", 20));
arr.AddRange(new Person[] { new Person("Jun", 32), new Person("Julie", 25), new Person("joe", 61) });
foreach (Person p in arr)
{
Console.WriteLine("Name : {0} , Age : {1}", p.name, p.age);
}
Console.ReadLine();
return 0;
}
}
}

ถ้าต้องการนับจำนวนสมาชิกใน ArrayList ก็เขียนได้ดังนี้

Console.WriteLine(arr.Count);

การเพิ่มสมาชิกเข้าไปใน ArrayList จะใช้เมธอด Insert ดังนี้

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication17
{
class Program
{
static int Main(string[] args)
{
ArrayList arr = new ArrayList();
arr.Add(new Person("Jane", 20));
arr.AddRange(new Person[] { new Person("Jun", 32), new Person("Julie", 25), new Person("joe", 61) });
Console.WriteLine("*****Before Call Insert method*****");
foreach (Person p in arr)
{
Console.WriteLine("Name : {0} , Age : {1}", p.name, p.age);
}
Console.WriteLine("\n*****After Call Insert Method*****");
arr.Insert(4, new Person("John", 33)); // กำหนดตำแหน่งที่ต้องการ insert และกำหนดข้อมูลที่ต้องการ insert
foreach (Person p in arr)
{
Console.WriteLine("Name : {0} , Age : {1}", p.name, p.age);
}
Console.ReadLine();
return 0;
}
}
}

การใช้งาน Stack

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication17
{
class Program
{
static int Main(string[] args)
{
Stack s = new Stack();
s.Push("A"); // เพื่มสมาชิกเข้าไปใน Stack
s.Push("B");
Console.WriteLine(s.Pop()); // แสดงข้อมูล
Console.WriteLine(s.Pop());
Console.ReadLine();
return 0;
}
}
}

เราอาจจะใช้ properties ที่ชื่อ Current ของ IEnemerator ในการแสดงข้อมูลก็ได้ดังนี้

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication17
{
class Program
{
static int Main(string[] args)
{
Stack s = new Stack();
s.Push("A"); // เพื่มสมาชิกเข้าไปใน Stack
s.Push("B");
IEnumerator i = s.GetEnumerator();
while (i.MoveNext())
{
Console.WriteLine(i.Current);
}
Console.ReadLine();
return 0;
}
}
}

การใช้งาน Queue
ในตัวอย่างนี้จะใช้ คลาส Person ที่สร้างไว้ในตอนแรก ใน Program.cs ให้เพิ่มเมธอดเข้าไปดังนี้

public static void ShowData(Person p)
{
Console.WriteLine("Name : {0} , Age : {1}", p.name, p.age);
}

แล้วใน Main ก็เขียนโค้ดดังนี้

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication17
{
class Program
{
static int Main(string[] args)
{
Queue q = new Queue();
q.Enqueue(new Person("Ann", 35));
q.Enqueue(new Person("Aon", 45));
ShowData((Person)q.Dequeue());
ShowData((Person)q.Dequeue());
Console.ReadLine();
return 0;
}
public static void ShowData(Person p)
{
Console.WriteLine("Name : {0} , Age : {1}", p.name, p.age);
}
}
}

การใช้งาน HashTable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication17
{
class Program
{
static int Main(string[] args)
{
Hashtable h = new Hashtable();
h.Add("A", 65);
h.Add("B", 66);
h.Add("C", 67);
foreach (string key in h.Keys)
{
Console.WriteLine(key); // แสดง Key
}
foreach (int value in h.Values)
{
Console.WriteLine(value);
}
Console.ReadLine();
return 0;
}

}
}

การสร้าง Collections ขึ้นมาใช้เอง
ในตัวอย่างนี้จะสร้างคลาส PersonCollection โดยในคลาสนี้จะต้องมีการ implement IEnumerable ด้วย
ดังนี้ ให้คลิกขวาที่ ชื่อโปรเจคเลือก Add -> Class ตั้งชื่อเป็น PersonCollection ในคลาสนี้เขียนโค้ดดังนี้
ไฟล์ PersonCollection.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication17
{
class PersonCollection : IEnumerable
{
public ArrayList arr = new ArrayList();
public Person GetPersonList(int i)
{
return (Person)arr[i]; //แสดงข้อมูลในตำแหน่ง ที่ i
}
public void AddPersonList(Person p)
{
arr.Add(p); // เพื่ม ข้อมูลชนิด object ของคลาส Person เข้าไปใน ArrayList
}
public int CountPerson
{
get
{
return arr.Count; // นับจำนวนสมาชิกใน ArrayList
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return arr.GetEnumerator(); //สำหรับการวนลูปโดยใช้ foreach
}
}
}

ไฟล์ Person.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication17
{
public class Person
{
public string name;
public int age;

public Person()
{
}
public Person(string name,int age)
{
this.name = name;
this.age = age;
}
public override string ToString()
{
return string.Format(name + " " + age);

}
}
}

ในไฟล์ Program.cs เขียนโค้ดดังนี้

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication17
{
class Program
{
static int Main(string[] args)
{
PersonCollection p = new PersonCollection();
p.AddPersonList(new Person("Ann", 44));
p.AddPersonList(new Person("John", 33));
p.AddPersonList(new Person("Jane", 47));
foreach (Person pp in p)
{
Console.WriteLine(pp.name + " " + pp.age);
}
Console.ReadLine();
return 0;
}
}
}

การใช้งาน BitVector32
BitVector32 นี้จะทำหน้าที่เก็บข้อมูลที่เป็น Booleanและ int ขนาดเล็ก ในหน่วยความจำ 32 bit
ในการใช้งาน BitVector32 จะต้อง using System.Collections.Specialized; ด้วย
ตัวอย่าง

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace ConsoleApplication17
{
class Program
{
static void Main()
{
BitVector32 bit1 = new BitVector32(1);
BitVector32 bit2 = new BitVector32(0);
Console.WriteLine(bit1.Data);
Console.WriteLine(bit2.Data);
Console.ReadLine();
}
}
}

การใช้งาน NameValueCollection
จะเก็บข้อมูลที่อยู่ในรูปแบบ key,value โดย key,value จะต้องเป็นชนิด String ในการใช้งาน NameValueCollection จะต้อง using System.Collections.Specialized; ด้วย
ตัวอย่าง

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace ConsoleApplication17
{
class Program
{
static void Main()
{
NameValueCollection name = new NameValueCollection();
name.Add("1","A");
name.Add("2", "B");
name.Add("3", "C");
foreach (string ii in name.AllKeys)
{
Console.WriteLine(ii);
}
Console.ReadLine();
}
}
}

การใช้งาน StringCollection
StringCollection จะเป็นการสร้างกลุ่มของ String ขึ้นมา
ตัวอย่าง

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace ConsoleApplication17
{
class Program
{
static void Main()
{
string s1 = "AA";
string s2 = "BB";
string[] str = { "CC", "CC", "EE" };
StringCollection col = new StringCollection();
col.Add(s1);
col.Add(s2);
col.AddRange(str);
StringEnumerator i = col.GetEnumerator(); // ใช้ StringEnumerator แทน IEnumerator
while (i.MoveNext())
{
Console.WriteLine(i.Current);
}
Console.ReadLine();
}
}
}

การใช้งาน HybridDictionary
คล้ายๆกับ Hashtable แต่เก็บกลุ่มของข้อมูลขนาดเล็ก
ตัวอย่าง

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace ConsoleApplication17
{
class Program
{
static void Main()
{
HybridDictionary hb = new HybridDictionary();
hb.Add(1, "A");
hb.Add(2, "B");
hb.Add(3, "C");
foreach (int s in hb.Keys)
{
Console.WriteLine(s);
}
foreach (string s in hb.Values)
{
Console.WriteLine(s);
}
Console.WriteLine();
Hashtable ht = new Hashtable(hb);
foreach (int s in ht.Keys)
{
Console.WriteLine(s);
}
foreach (string s in ht.Values)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}
# รู้ไว้ใช่ว่า จำใส่สมองได้บ้างนิดหน่อยก็คงดีนะเรา ^^'
# แ่ต่ประเด็นก็ไม่ได้อยู่แค่ จำได้นำไปใช้ให้คอมไพล์ผ่านแค่นั้น
# สำคัญสุด เราจะเลือกใช้ collection ตัวใหน ที่จะเหมาะกับ process ของเรามากกว่า
# อันนี้อ่ะ ก็คิดซะหน่อยเพื่อ performance ของ งานเรา

No comments:

Post a Comment

Popular Posts