This
article will show you how you can prepare a dictionary and perform search
operation for value by using key name in c#.net.
Some of my
previous articles are as follows: How
to Extract Text From PDF File Using C#.Net, How
to Read a Text File Line by Line in C# in Console Application, Retrieve
All Files Present inside a Directory & Sub Directory Using C#.Net.
So for this
article we will create a new console application and add the below code.
using System;
using
System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
namespace
ConsoleApplication1
{
class Program
{
//c# dictionary get value by key
static void Main(string[] args)
{
Console.WriteLine("Search For: Pakistan\n");
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("India", 1);
dictionary.Add("Pakistan", 2);
dictionary.Add("Sri Lanka", 3);
dictionary.Add("America", 4);
if (dictionary.ContainsKey("Pakistan"))
{
int value = dictionary["Pakistan"];
Console.WriteLine("Pakistan value is : " + value);
}
Console.WriteLine("\n\n------End Search--------\n");
Console.ReadLine();
}
}
}
|
In above
code I have prepared the dictionary and then added some value. After that I have
search the value by key name. Hear I have shown example for Pakistan.
0 comments:
Please let me know your view