ArrayList myList = new ArrayList ();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add("4");
foreach (Object obj in myList)
{
int number = (int)obj;
}
Type | Generic Type |
---|---|
ArrayList | List<> |
Queue | Queue<> |
Stack | Stack<> |
Hashtable | Dictionary<> |
SortedList | SortedList<> |
ListDictionary | Dictionary<> |
OrderedDictionary | Dictionary<> |
SortedDictionary | SortedDictionary<> |
NameValueCollection | Dictionary<> |
DictionaryEntry | NameValuePair<> |
StringCollection | List<String> |
StringDictionary | Dictionary<String> |
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public double EmpSalary { get; set; }
public Employee()
{
EmpID = 0;
EmpName = "N/A";
EmpSalary = 0.0;
}
public Employee(int ID, string Name ,double salary)
{
EmpID = ID;
EmpName = Name;
EmpSalary = salary;
}
public List<Employee> GetEmployees()
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee(1, "Raj ", 2000));
empList.Add(new Employee(2, "Sheetal", 1000));
empList.Add(new Employee(3, "Ramesh", 2500));
empList.Add(new Employee(4, "Shaheen", 3000));
return empList;
}
}
class Program
{
static void Main(string[] args)
{
Employee obj = new Employee();
Console.WriteLine("EmployeeID \t EmployeeName \t EmployeeSalary");
foreach(Employee emp in obj.GetEmployees())
{
Console.WriteLine(" {0} \t\t {1} \t\t {2}", emp.EmpID, emp.EmpName,
emp.EmpSalary );
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SortedList<int, string> myList = new SortedList<int, string>();
myList.Add(30, "Thirty");
myList.Add(40, "Fourty");
myList.Add(10, "Ten");
myList.Add(50, "Fifty");
myList.Add(20, "Twenty");
foreach (KeyValuePair<int, string> item in myList)
{
Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dicObj = new Dictionary<int, string>();
dicObj.Add(1, "One");
dicObj.Add(2, "Two");
dicObj.Add(3, "Three");
dicObj.Add(4,"Four");
foreach (KeyValuePair<int, string> item in dicObj)
{
Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
}
Console.ReadLine();
}
}
}
Dictionary<int, string> dicObj = new Dictionary<int, string>()
{
{1,"One"},
{2,"Two"},
{3,"Three"},
{4,"Four"}
};
Property | Description |
---|---|
Count | Gets the total number of items exists in the Dictionary. |
IsReadOnly | Returns a boolean indicating whether the Dictionary<TKey,TValue> is read-only. |
Keys | Returns collection of keys of Dictionary<TKey,TValue>. |
Values | Returns collection of values in Dictionary<TKey,TValue>. |
Methods | Description |
---|---|
void Add(TKey key, TValue value) | Add key-value pairs in Dictionary<TKey, TValue> collection. |
void Remove(TKey) | Removes the element with the specified key. |
bool ContainsKey(TKey key) | It checks whether the value exists or not. |
bool ContainsValue(TValue value) | It checks whether the value exists or not. |
void Clear() | Removes all the elements from generic Dictionary. |