ÀÌ ¹®¼¿¡¼´Â ÇØ½Ã Ä÷º¼ÇÀ» »ç¿ëÇÏ´Â ¹æ¹ýÀ» º¸¿© ÁÝ´Ï´Ù. ÇØ½Ã µ¥ÀÌÅ͸¦ °Ë»öÇÏ´Â µ¥ ºñ¿ëÀÌ ¸¹ÀÌ µå´Â µ¥ÀÌÅÍ °Ë»ö Çʿ䰡 ¾øÀ¸¹Ç·Î ÇØ½Ã µ¥ÀÌÅ͸¦ È¿À²ÀûÀ¸·Î °Ë»öÇÒ ¼ö ÀÖ½À´Ï´Ù. ÇØ½Ã Ű ÀÚü´Â °ªÀ» »ç¿ëÇÏ¿© µ¥ÀÌÅ͸¦ ã½À´Ï´Ù.
±âº» Ŭ·¡½º ¶óÀ̺귯¸®¸¦ Á÷Á¢ ÇØ½Ã Å×À̺í ÄÚµå ÇÊ¿äÇÏÁö ¾ÊÀº ÀÖµµ·Ï
System.Collections ³×ÀÓ½ºÆäÀ̽º¿¡ Á¤ÀǵÈ
ÇØ½Ã Ŭ·¡½º¸¦ Á¦°øÇÕ´Ï´Ù.
´Ü°è¸¦ »ùÇÃÀ» ºôµåÇÏ·Á¸é
ÇØ½Ã Ä÷º¼Ç
(Ű, °ª) ½ÖÀÌ ÀúÀåÇϰí
Ű ÇØ½Ã ¹× ÀúÀå¼Ò À§Ä¡¸¦ °¡Á®¿À´Â µ¥ »ç¿ëÇÕ´Ï´Ù.
Ű º¯°æÇÒ ¼ö ¹× Áߺ¹ Ç׸ñÀÌ ÀÖ´Â
ÇØ½Ã °¡Áú ¼ö ¾ø½À´Ï´Ù. ÀÌ »ùÇÿ¡¼´Â °£´ÜÇÑ
Person Ŭ·¡½º ÀνºÅϽº°¡ ¿©·¯ °³¸¦ ÀÖ´Â
ÇØ½Ã ÀúÀåÇÏ´Â ¿¹Á¦ÀÔ´Ï´Ù. ¼ºÀº
Ű ·Î »ç¿ëµË´Ï´Ù.
- Microsoft Visual Studio¸¦ ¿°í Windows Forms ÀÀ¿ë ÇÁ·Î±×·¥ ÇÁ·ÎÁ§Æ®¸¦ Visual C# ¿¡¼ ¸¸µì´Ï´Ù. ±âº»ÀûÀ¸·Î Form1ÀÌ ÇÁ·ÎÁ§Æ®¿¡ Ãß°¡µË´Ï´Ù.
- ¼Ö·ç¼Ç Ž»ö±â¿¡¼ ÇÁ·ÎÁ§Æ® À̸§À» ¸¶¿ì½º ¿À¸¥ÂÊ ´ÜÃß·Î, Ãß°¡ ¸¦ °¡¸®Å² ´ÙÀ½ Ŭ·¡½º Ŭ·¡½º ¸ðµâ Ãß°¡ Ŭ¸¯ÇϽʽÿÀ. Class1 ±âº»ÀûÀ¸·Î ÇÁ·ÎÁ§Æ®¿¡ Ãß°¡µË´Ï´Ù.
- Class1 ¸ðµâÀÇ Äڵ带 ´ÙÀ½ ÄÚµå·Î ¹Ù²ß´Ï´Ù:
public class Person
{
public string Fname, Lname;
public Person(string FirstName, string LastName)
{
Fname = FirstName;
Lname = LastName;
}
public override string ToString()
{
return Fname + " " + Lname;
}
}
Person Ŭ·¡½º À̸§ ¹× ¼º ¸Å°³ º¯¼ö ¹× ÀÌ·¯ÇÑ ¸Å°³ º¯¼ö¸¦ ·ÎÄà º¯¼ö¿¡ ÇÒ´çÇÏ´Â ÇϳªÀÇ »ý¼ºÀÚ°¡ ÀÖ½À´Ï´Ù. ToString ÇÔ¼ö¸¦ ToStringÀ»Fname ¹× ÇÔ²² ¿¬°áµÈ Lname µ¹¾Æ°¡·Á¸é Object Ŭ·¡½º¿¡¼ ÀçÁ¤ÀÇÇÕ´Ï´Ù. - Æû ¼öÁØ Hashtable °³Ã¼¸¦ ¸¸µé°í »ç¶÷ Çü½ÄÀÇ ¼¼ °³ÀÇ º¯¼ö¸¦ ¼±¾ðÇϽʽÿÀ. Form1 Ŭ·¡½º¿¡ ´ÙÀ½ Äڵ带 Ãß°¡ÇÏ¿©:
<?xm-deletion_mark author="v-bobbid" time="20080711T172143-0800" data="private Hashtable MyTable = new Hashtable();
//For simplicity, create three Person objects to add to the HashTable collection.
Person Person1,Person2,Person3; "?><?xm-insertion_mark_start author="v-bobbid" time="20080711T172143-0800"?>System.Collections.Hashtable MyTable = new
System.Collections.Hashtable();
//For simplicity, create three Person objects to add to the HashTable collection.
Person Person1,Person2,Person3;
<?xm-insertion_mark_end?>
- ´ÙÀ½ ´Ü°è¿¡¼ try-catch ºí·Ï¿¡ Hashtable ¼¼ »ç¶÷ °³Ã¼¸¦ Ãß°¡ÇÏ·Á¸é Hashtable °³Ã¼ÀÇ Add ¸Þ¼µå¸¦ »ç¿ëÇϽʽÿÀ. try-catch ºí·Ï¿¡ ¿¹¿Ü¸¦ catchÇÑ ´ÙÀ½ Áߺ¹ ۰¡ Á¸ÀçÇÒ °æ¿ì ¸Þ½ÃÁö¸¦ Ç¥½ÃÇÏ´Â:
- Form1 Button ÄÁÆ®·ÑÀ» ¹èÄ¡Çϰí Ç׸ñ Ãß°¡Text ¼Ó¼ºÀ» º¯°æÇϽʽÿÀ.
- ÇØ´ç ÄÚµå â ¿±â ´ÜÃ߸¦ µÎ ¹ø Ŭ¸¯Çϰí Button1_Click À̺¥Æ®¿¡ ´ÙÀ½ Äڵ带 ºÙ¿©:
Person1 = new Person("David", "Burris");
Person2 = new Person("Johnny", "Carrol");
Person3 = new Person("Ji", "Jihuang");
//The Add method takes Key as the first parameter and Value as the second parameter.
try
{
MyTable.Add(Person1.Lname, Person1);
MyTable.Add(Person2.Lname, Person2);
MyTable.Add(Person3.Lname, Person3);
}
catch (ArgumentException ae)
{
MessageBox.Show("Duplicate Key");
MessageBox.Show(ae.Message);
}
- Hashtable °³Ã¼´Â Àε¦¼¸¦ Á¦°øÇÕ´Ï´Ù. ´ÙÀ½ ´Ü°è¿¡¼, ÇØ½ÃµÈ À§Ä¡¿¡ ÀúÀåµÈ °ª¿¡ ¾×¼¼½ºÇÏ´Â µ¥ Ű À妽º:
- Ç׸ñ °¡Á®¿À±âName ¼Ó¼ºÀ» º¯°æÇϰí, Form1¿¡ Button ÄÁÆ®·ÑÀ» Ãß°¡ÇϽʽÿÀ.
- ´ÜÃ߸¦ µÎ ¹ø ¹× Button2_Click À̺¥Æ®¿¡ ´ÙÀ½ Äڵ带 ºÙ¿©:
//Use the indexer of the Hashtable class to retrieve your objects. The indexer takes
//Key as a parameter and accesses it with the Hashed location.
try
{
MessageBox.Show(MyTable[Person1.Lname].ToString());
MessageBox.Show(MyTable[Person2.Lname].ToString());
MessageBox.Show(MyTable[Person3.Lname].ToString());
}
catch (NullReferenceException ex)
{
MessageBox.Show("Key not in Hashtable");
MessageBox.Show(ex.Message);
}
- ´ÙÀ½ ´Ü°è´Â ÇØ½Ã Ä÷º¼Ç¿¡¼ ´ÜÀÏ Ç׸ñÀ» Á¦°ÅÇÏ·Á¸é Remove ¸Þ¼µå¸¦ »ç¿ëÇϽʽÿÀ.
- Ç׸ñ Á¦°ÅText ¼Ó¼ºÀ» º¯°æÇϰí, Form1¿¡ Button ÄÁÆ®·ÑÀ» Ãß°¡ÇϽʽÿÀ.
- ´ÜÃ߸¦ µÎ ¹ø Ŭ¸¯ÇÑ Button3_Click À̺¥Æ®¿¡ ´ÙÀ½ Äڵ带 ºÙ¿©:
<?xm-deletion_mark author="v-bobbid" time="20080711T173011-0800" data="if (MyTable.Count == 0)
{
MessageBox.Show("There are no items in HashTable");
}
else
{
MessageBox.Show("The count before removing an Item is" + " " + MyTable.Count);
MessageBox.Show("Removing value stored at key value (Burris)");
Remove the object that is stored at the Key value Person1.Lname.
MyTable.Remove(Person1.Lname);
}"?><?xm-insertion_mark_start author="v-bobbid" time="20080711T173011-0800"?>if (MyTable.Count == 0)
{
MessageBox.Show("There are no items in HashTable");
}
else
{
MessageBox.Show("The count before removing an Item is" + " " + MyTable.Count);
MessageBox.Show("Removing value stored at key value (Burris)");
// Remove the object that is stored at the Key value Person1.Lname.
MyTable.Remove(Person1.Lname);
}
<?xm-insertion_mark_end?>
- ´ÙÀ½ ´Ü°è´Â ÇØ½Ã Ä÷º¼Ç¿¡ ÀúÀåµÈ Ç׸ñÀ» ¿°ÅÇÒ:
- ¿°Å ¼ö Text ¼Ó¼ºÀ» º¯°æÇϰí, Form1¿¡ Button ÄÁÆ®·ÑÀ» Ãß°¡ÇϽʽÿÀ.
- ´ÜÃ߸¦ µÎ ¹ø ¹× Button4_Click À̺¥Æ®¿¡ ´ÙÀ½ Äڵ带 ºÙ¿©:
<?xm-deletion_mark author="v-bobbid" time="20080711T174252-0800" data="IDictionaryEnumerator Enumerator;
if (MyTable.Count == 0)
MessageBox.Show("The hashtable is empty");
else
{
MessageBox.Show("Enumerating through the Hashtable collection");
Enumerator = MyTable.GetEnumerator();
while (Enumerator.MoveNext())
{
MessageBox.Show(Enumerator.Value.ToString());
}
}
ICollection MyKeys;
if (MyTable.Count == 0)
MessageBox.Show("The hashtable is empty");
else
{
MessageBox.Show("Accessing keys property to return keys collection");
MyKeys = MyTable.Keys;
foreach (object Key in MyKeys)
{
MessageBox.Show(Key.ToString());
}
}
"?><?xm-insertion_mark_start author="v-bobbid" time="20080711T174252-0800"?>System.Collections.IDictionaryEnumerator Enumerator;
if (MyTable.Count == 0)
MessageBox.Show("The hashtable is empty");
else
{
MessageBox.Show("Enumerating through the Hashtable collection");
Enumerator = MyTable.GetEnumerator();
while (Enumerator.MoveNext())
{
MessageBox.Show(Enumerator.Value.ToString());
}
}
System.Collections.ICollection MyKeys;
if (MyTable.Count == 0)
MessageBox.Show("The hashtable is empty");
else
{
MessageBox.Show("Accessing keys property to return keys collection");
MyKeys = MyTable.Keys;
foreach (object Key in MyKeys)
{
MessageBox.Show(Key.ToString());
}
}
<?xm-insertion_mark_end?> ÀÌ ÄÚµå´Â IDictionaryEnumerator Çü½ÄÀÇ º¯¼ö¸¦ ¼±¾ðÇϰí ÇØ½Ã Ä÷º¼ÇÀÇ GetEnumerator ¸Þ¼µå¸¦ È£ÃâÇÕ´Ï´Ù. ¹ÝȯµÈ Ç¥½Ã±â ¸¦ »ç¿ëÇÏ¿© Äڵ带 ÅëÇØ Ä÷º¼Ç¿¡ ÀÖ´Â Ç׸ñÀÇ ¿°ÅÇϰí ÇØ½ÃŰ ¸Þ¼µå¸¦ ÅëÇØ ۸¦ ¿°ÅÇÏ´Â µ¥ »ç¿ëÇÕ´Ï´Ù.
- ´ÙÀ½ ´Ü°è¿¡¼ ÇØ½Ã Áö¿ì·Á¸é Áö¿ì±â ¸Þ¼µå¸¦ »ç¿ëÇϽʽÿÀ.
- Form1¿¡ Button ÄÁÆ®·ÑÀ» Ãß°¡Çϰí Text ¼Ó¼ºÀ» Áö¿ì±â º¯°æÇϽʽÿÀ.
- ´ÜÃ߸¦ µÎ ¹ø ¹× Button5_Click À̺¥Æ®¿¡ ´ÙÀ½ Äڵ带 ºÙ¿©:
MyTable.Clear();
MessageBox.Show("HashTable is now empty");
- ºôµå ¹× ÀÀ¿ë ÇÁ·Î±×·¥À» ½ÇÇàÇÏ·Á¸é ´ÙÀ½°ú °°ÀÌ ÇϽʽÿÀ.
- Ç׸ñ Ãß°¡ Ŭ¸¯ÇϽʽÿÀ. ¼¼ »ç¶÷ °³Ã¼¸¦ ÇØ½Ã Ä÷º¼Ç¿¡ Ãß°¡µÇ´Â À¯ÀÇÇϽʽÿÀ.
- Ç׸ñ °¡Á®¿À±â ¸¦ Ŭ¸¯ÇϽʽÿÀ. Àε¦¼¸¦ ÇØ½Ã Ä÷º¼Ç¿¡ ÀÖ´Â Ç׸ñÀÇ ¾òÀº À¯ÀÇÇϽʽÿÀ. Ç׸ñÀÌ Ç¥½ÃµÇ´Â ¼¼ °¡Áö »õ·Î Ãß°¡µÇ¾ú½À´Ï´Ù.
- Ç׸ñ Á¦°Å ¸¦ Ŭ¸¯ÇϽʽÿÀ. "Burris" Ű À§Ä¡¿¡ ÀÖ´Â Ç׸ñÀ» »èÁ¦ÇÒ À¯ÀÇÇϽʽÿÀ.
- ¿°Å ¸¦ ´©¸¨´Ï´Ù. ÇØ½Ã Ä÷º¼ÇÀÇ Ç׸ñÀ» ÅëÇØ IDictionaryEnumerator ¿°ÅÇÕ´Ï´Ù ¹× Ű Ä÷º¼Ç, ÇØ½Ã Ű ¼Ó¼ºÀ» ¹ÝȯÇÏ´Â À¯ÀÇÇϽʽÿÀ.
- Áö¿ì±â ¸¦ Ŭ¸¯ÇϽʽÿÀ. ÇØ½Ã Ä÷º¼Ç¿¡¼ ¸ðµç Ç׸ñÀ» Áö¿ì·Á¸é À¯ÀÇÇϽʽÿÀ.
Âü°í ȸ»ç, Á¶Á÷, Á¦Ç°, µµ¸ÞÀÎ À̸§, ÀüÀÚ ¸ÞÀÏ ÁÖ¼Ò, ·Î°í, »ç¶÷, Àå¼Ò, ¹× À̺¥Æ® µîÀº ½ÇÁ¦ µ¥ÀÌÅͰ¡ ¾Æ´Õ´Ï´Ù. ¸ðµç ½ÇÁ¦ ȸ»ç, Á¶Á÷, Á¦Ç°, µµ¸ÞÀÎ À̸§, ÀüÀÚ ¸ÞÀÏ ÁÖ¼Ò, ·Î°í, »ç¶÷, Àå¼Ò ¶Ç´Â ¿¬°áµÈ ÀÛ¼ºµÇ¾ú½À´Ï´Ù À¯ÃßÇØ¼µµ ¾ÈµË´Ï´Ù.
±â¼ú ÀÚ·á: 309357 - ¸¶Áö¸· °ËÅä: 2008³â 7¿ù 15ÀÏ È¿äÀÏ - ¼öÁ¤: 4.0
º» ¹®¼ÀÇ Á¤º¸´Â ´ÙÀ½ÀÇ Á¦Ç°¿¡ Àû¿ëµË´Ï´Ù.
- Microsoft Visual C# 2008 Express Edition
- Microsoft Visual C# 2005
- Microsoft Visual C++ .NET 2003 Standard Edition
- Microsoft Visual C# .NET 2002 Standard Edition
| kbmt kbhowtomaster KB309357 KbMtko |
±â°è ¹ø¿ªµÈ ¹®¼Áß¿ä: º» ¹®¼´Â Àü¹® ¹ø¿ª°¡°¡ ¹ø¿ªÇÑ °ÍÀÌ ¾Æ´Ï¶ó Microsoft ±â°è ¹ø¿ª ¼ÒÇÁÆ®¿þ¾î·Î ¹ø¿ªÇÑ °ÍÀÔ´Ï´Ù. Microsoft´Â ¹ø¿ª°¡°¡ ¹ø¿ªÇÑ ¹®¼ ¹× ±â°è ¹ø¿ªµÈ ¹®¼¸¦ ¸ðµÎ Á¦°øÇϹǷΠMicrosoft ±â¼ú ÀÚ·á¿¡ ÀÖ´Â ¸ðµç ¹®¼¸¦ Çѱ۷ΠÁ¢ÇÒ ¼ö ÀÖ½À´Ï´Ù. ±×·¯³ª ±â°è ¹ø¿ª ¹®¼°¡ Ç×»ó ¿Ïº®ÇÑ °ÍÀº ¾Æ´Õ´Ï´Ù. µû¶ó¼ ±â°è ¹ø¿ª ¹®¼¿¡´Â ¸¶Ä¡ ¿Ü±¹ÀÎÀÌ Çѱ¹¾î·Î ¸»ÇÒ ¶§ ½Ç¼ö¸¦ ÇÏ´Â °Íó·³ ¾îÈÖ, ±¸¹® ¶Ç´Â ¹®¹ý¿¡ ¿À·ù°¡ ÀÖÀ» ¼ö ÀÖ½À´Ï´Ù. Microsoft´Â ³»¿ë»óÀÇ ¿À¿ª ¶Ç´Â Microsoft °í°´ÀÌ ÀÌ·¯ÇÑ ¿À¿ªÀ» »ç¿ëÇÔÀ¸·Î½á ¹ß»ýÇÏ´Â ºÎ Á¤È®¼º, ¿À·ù ¶Ç´Â ¼ÕÇØ¿¡ ´ëÇØ Ã¥ÀÓÀ» ÁöÁö ¾Ê½À´Ï´Ù. Microsoft´Â ÀÌ·¯ÇÑ ¹®Á¦¸¦ ÇØ°áÇϱâ À§ÇØ ±â°è ¹ø¿ª ¼ÒÇÁÆ®¿þ¾î¸¦ ÀÚÁÖ ¾÷µ¥ÀÌÆ®Çϰí ÀÖ½À´Ï´Ù.