Generic Collections:
- Are Type Safe.
- Variable length (Auto Resizing)
ArrayList VS HashTable
ArrayList | Hash Table |
1123 | Id: 1123 |
“Adil” | Name: “Adil” |
“Manager” | Designation: “Manager” |
25000 | Salary: 25000 |
923043102345 | ContactNo: 923043102345 |
“Hyderabad” | City: “Hyderabad” |
“Unit No 7” | Address: “Unit No 7” |
“June 2018” | HireDate: “June 2018” |
C# – Dictionary
- Now we want to create a collection in which we don’t want to access elements through index.
- We want to store data in key value format where keys are user-defined.
- We want to insert same type of data in a collection.
DICTIONARY<TKey, TValue>
“active”: ”ready to engage in physically energetic pursuits”,
“amazing”: “causing great surprise or wonder”,
“honest”: “free of deceit, truthful and sincere”,
“patriotic”: “expressing devotion to and vigorous support for one's country.”,
“quarantine”: “place of isolation”
- In C#, Dictionary is a generic collection which is generally used to store key/value pairs.
- The working of Dictionary is quite similar to the non-generic hashtable.
- The advantage of Dictionary is, it is generic type.
- Dictionary is defined under System.Collection.Generic namespace.
- It is dynamic in nature means the size of the dictionary is grows according to the need.
- The Dictionary collection in C# is same as English dictionary.
- English dictionary is a collection of words and their definitions, often listed alphabetically in one or more specific languages.
- In the same way, the Dictionary in C# is a collection of Keys and Values, where key is like word and value is like definition.
- The Dictionary class is a generic collection class in the System.Collection.Generics namespace.
- TKey denotes the type of key and TValue is the type of TValue.
Important Properties Of Dictionary
- Count: Gets the total number of elements exists in the Dictionary.
- Keys: Returns collection of keys of Dictionary.
- Values: Returns collection of values in Dictionary.
Important METHODS Of Dictionary
- Add Adds an item to the Dictionary collection,Add key- value pairs in Dictionary collection.
- Remove Removes the element with the specified key.
- ContainsKey Checks whether the specified key exists in Dictionary.
- ContainsValue Checks whether the specified key exists in Dictionary.
- Clear Removes all the elements from Dictionary.
- TryGetValue Returns true and assigns the value with specified key, if key does not exists then return false.
Important Points
- In Dictionary, the key cannot be null, but value can be.
- In Dictionary, key must be unique. Duplicate keys are not allowed if you try to use duplicate key then compiler will throw an exception.
- In Dictionary, you can only store same types of elements.
- The capacity of a Dictionary is the number of elements that Dictionary can hold.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DictionaryGenericCollection
{
class Program
{
static void Main(string[] args)
{
//Dictionary<string, string> myDict = new Dictionary<string, string>();
//myDict.Add("active", "ready to engage in physically energetic pursuits");
//myDict.Add("amazing", "causing great surprise or wonder");
//myDict.Add("honest", "free of deceit, truthful and sincere");
//myDict.Add("patriotic", "expressing devotion to and vigorous support for one's country.");
//myDict.Add("quarantine", "place of isolation");
//Employee emp1 = new Employee()
//{
// id = 111,
// name = "Ali",
// designation = "Manager",
// salary = 15000
//};
//Employee emp2 = new Employee()
//{
// id = 222,
// name = "Asad",
// designation = "Assistant",
// salary = 12000
//};
//Employee emp3 = new Employee()
//{
// id = 333,
// name = "Zain",
// designation = "Operator",
// salary = 8000
//};
//Dictionary<int, Employee> myEmployees = new Dictionary<int, Employee>();
//myEmployees.Add(emp1.id, emp1);
//myEmployees.Add(emp2.id, emp2);
//myEmployees.Add(emp3.id, emp3);
//Console.WriteLine(myEmployees.Count(emp => emp.Value.name.StartsWith("B")));
//Console.WriteLine(myDict.Count());
//Dictionary<int, string> myDict2 = new Dictionary<int, string>();
//myDict2.Add(1, "Adil");
//Console.WriteLine(myDict["active"]);
Dictionary<string, string> myDict = new Dictionary<string, string>();
myDict.Add("active", "ready to engage in physically energetic pursuits");
myDict.Add("amazing", "causing great surprise or wonder");
myDict.Add("honest", "free of deceit, truthful and sincere");
myDict.Add("patriotic", "expressing devotion to and vigorous support for one's country.");
myDict.Add("quarantine", "place of isolation");
if (myDict.ContainsKey("quarantine"))
{
Console.WriteLine("Key Found");
}
else
{
Console.WriteLine("Key not Found");
}
//string value;
//myDict.TryGetValue("honest", out value);
//Console.WriteLine(value);
//foreach (KeyValuePair<string, string> item in myDict)
//{
// Console.WriteLine("Key is: " + item.Key + " Value is: " + item.Value);
//}
//myDict.Clear();
//Console.WriteLine("-----------------------");
//foreach (KeyValuePair<string, string> item in myDict)
//{
// Console.WriteLine("Key is: " + item.Key + " Value is: " + item.Value);
//}
//Console.WriteLine(myDict.ContainsValue("bmnbmnbmnb"));
//if(myDict.ContainsKey("quarantine"))
//{
// Console.WriteLine("Key Found");
//}
//else
//{
// Console.WriteLine("Key Not Found");
//}
//Console.WriteLine(myDict.ContainsKey("Adil"));
//foreach (KeyValuePair<string, string> item in myDict)
//{
// Console.WriteLine("Key is: " + item.Key + " Value is: " + item.Value);
//}
//myDict.Remove("quarantine");
//Console.WriteLine("-----------------------");
//foreach (KeyValuePair<string, string> item in myDict)
//{
// Console.WriteLine("Key is: " + item.Key + " Value is: " + item.Value);
//}
//foreach (string key in myDict.Keys)
//{
// Console.WriteLine(key);
//}
//foreach (string value in myDict.Values)
//{
// Console.WriteLine(value);
//}
Console.ReadLine();
}
}
}
Click Below Link to Download Notes Of This Blog
https://www.mediafire.com/file/c7i3evwewuk45s0/DICTIONARY+GENERIC+COLLECTION+IN+C#.pptx/file
Click Below Link to Download Source Code Project Of This Blog
https://www.mediafire.com/file/aitt3dqsxm8lkm2/DictionaryGenericCollection.rar/file
No responses yet