At more complex, an object holds references to many other objects. The collections we often using is List and Dictionary.
Different between stacks, queues, lists, and hash tables
Declare a Dictionary
When you Create a Dictionary instance, you needed to pass in <TYPE,TYPE>,to tell that which type of Dictionary you want to Create.See Also GENERICS
Eg
Dictionary<TYPE, TYPE> objectInstance = new Dictionary<TYPE, TYPE>();
Example(Here i'm using Dictionary)
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace IntroduceCollection
{
class IntroduceCollection
{
static void Main()
{
Dictionary<string, string> carPlateNum = new Dictionary<string, string>()
{
{"john", "JKE 1333"},
{"jess", "QQ 1314"},
{"apple", "JK 8888"},
{"jack", "USS 1555"},
{"alice","AC 6666"}
};
// Using LIST to store car OwnerName
List<string> ownerName = new List<string>(carPlateNum.Keys);
/* Output all the CarPlate Data *
* */
Console.WriteLine("Owner\tPlate Number");
foreach (string name in ownerName)
{
Console.WriteLine("{0}\t{1}", name, carPlateNum[name]);
}
//end Output with using LIST
//Direct output
Console.WriteLine("Foreach looping Output content of Dictionary");
foreach (KeyValuePair<string, string> carInfo in carPlateNum)
{
Console.WriteLine("{0}\t{1}", carInfo.Key, carInfo.Value);
}
Console.ReadLine();
}
}
}
Example above i'm create a Dictionary to store Owner and Car Plate Number .Then output the information.
No comments:
Post a Comment