0% found this document useful (0 votes)
80 views

Dictionary in C# - Windows App Tutorials

The document discusses how to use dictionaries in C# code. It explains how to define a dictionary by specifying key and value types, add elements to a dictionary, read and update existing values, check if a key exists, iterate through dictionary elements, and remove elements from a dictionary. Code examples are provided for each of these common dictionary operations.

Uploaded by

Ivan Horvat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Dictionary in C# - Windows App Tutorials

The document discusses how to use dictionaries in C# code. It explains how to define a dictionary by specifying key and value types, add elements to a dictionary, read and update existing values, check if a key exists, iterate through dictionary elements, and remove elements from a dictionary. Code examples are provided for each of these common dictionary operations.

Uploaded by

Ivan Horvat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Windows App Tutorials HOME ABOUT TIPS WINDOWS PHONE WINDOWS 8 WRITE FOR US PROJECTS

Search...
Home » Tips » General » Dictionary in C#

Buy me a coffee

ADVERTISMENT


Dictionary in C#
 March 15, 2015  Vivek Maskara General 9,013 views

Tweet
Dictionary<TKey, TValue> provide fast lookups, based on keys, to get values. With them, we use
keys and values of any type, including int and string. Dictionary is used with di erent elements. We
0 specify its key type and its value type (string, int).

1
De ne a Dictionary
Like We specify its key type and its value type (string, int).

1 Dictionary<string, int> dictionary = new Dictionary<string, int>();

SHARE gistfile1.cs hosted with ❤ by GitHub view raw

Add elements to a Dictionary TRANSLATE


Here’s how to add elements to a Dictionary .

1 dictionary.Add("Monday", 0); TRANSLATE


2 dictionary.Add("Tuesday", 0);
3 dictionary.Add("Wednesday", 0);
4 dictionary.Add("Thursday", 0);
5 dictionary.Add("Friday", 0);
6 dictionary.Add("Saturday", 0);
Search… 
7 dictionary.Add("Sunday", 0);

gistfile1.cs hosted with ❤ by GitHub view raw


FOLLOW US

Read and update existing values


To read a value use the following statement.
POPULAR POSTS
1 int value= dictionary["Monday"];

gistfile1.cs hosted with ❤ by GitHub view raw How to use Google AdMob in
Windows Phone app
June 19, 2014

To update an existing value simply use the following.


1 dictionary["Monday"] = 1; Using in App purchase to
implement `Block Ads’ Feature in
gistfile1.cs hosted with ❤ by GitHub view raw
Windows Phone app
June 22, 2014

Check if a key exists in a Dictionary


Adding Microsoft pubcenter ads
This sees if a given string is present in a Dictionary.
in Windows Phone 8 apps
1 if (dictionary.ContainsKey("Monday")) July 20, 2014
2 {
3 int value = dictionary["Monday"]; Working with local database in
4 Console.WriteLine(value); Windows Phone 8- Part III
5 } July 13, 2014

gistfile1.cs hosted with ❤ by GitHub view raw


Using Ad Mediation in Windows
Phone 8 app
Iterate through a Dictionary November 8, 2014

Here we loop over KeyValuePairs in a Dictionary . With collections like Dictionary , we must
always know the value types. With each KeyValuePair , there is a Key member and Value
member.
CATEGORIES
1 foreach (KeyValuePair<string, int> pair in dictionary)
2 {  Apps (4)
3 Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
 Code Snippets (15)
4 }

gistfile1.cs hosted with ❤ by GitHub view raw  Featured (3)

 News (2)
Remove elements from a Dictionary
 Suggestions (3)
Here’s how you can remove elements from a Dictionary .

1 if (dictionary.ContainsKey("Monday"))  Tips (56)


2 {
 General (41)
3 d.Remove("Monday");
4 }
 Images (2)
gistfile1.cs hosted with ❤ by GitHub view raw
 Listbox (2)

Count elements in a Dictionary  Media (1)

This computes the total number of keys in a Dictionary . This is simpler than accessing the Keys  Settings (2)
property, or looping over the Dictionary to count it.
 Speech (2)
1 int count= dictionary.Count;

gistfile1.cs hosted with ❤ by GitHub view raw  StorageFile (6)

Clear all elements from a Dictionary  Windows 10 (10)

We can erase all pairs with the Clear method.  UI (3)


1 dictionary.Clear();

gistfile1.cs hosted with ❤ by GitHub view raw  Windows 8 (6)

 Advertising (1)
Check if a value exists in a Dictionary
 General(Windows 8.1) (2)
This method lacks the constant-time look up speed of ContainsKey. It instead searches the entire
collection.  Media (Windows 8.1) (1)
1 if (d.ContainsValue(1))
 Networking(Windows 8.1) (1)
2 {
3 Console.WriteLine(true); // true
 Visual Basic (1)
4 }

gistfile1.cs hosted with ❤ by GitHub view raw


 Windows Phone (90)

Get all keys of a Dictionary  Advertising (6)

Here we use the Keys property. We then look through each key and look up the values.  Animations and E ects (7)
1 List<string> list = new List<string>(dictionary.Keys);
 App Studio (1)
2 // Loop through list
3 foreach (string k in list)
 Authentication (1)
4 {
5 Console.WriteLine("{0}, {1}", k, d[k]);  Basics (4)
6 }
 Bing Services (1)
gistfile1.cs hosted with ❤ by GitHub view raw

 Data binding (5)

 Data Handling (2)

 General (12)
Share this:

Share 1 Tweet Share Save  Email  Google Services (3)

 Isolated Storage (6)

Related
 Launchers and choosers (8)

 ListBox (6)

 Local Database (5)

 Location (2)
Using Browser Emulation for Using Shared Theme Isolated Storage in Windows
Web Browser control in WPF Resources in Universal phone app – Store data in  Media (6)
app Windows Apps IsolatedStorageSettings
June 23, 2016 January 21, 2016 June 27, 2014  Monetizing (8)
In "WPF" In "UI" In "Isolated Storage"
 Networking (2)

 Noti cations (3)


add C# de ne dictionary key value remove
 Telerik (2)

 Tiles (6)

 UI (5)

 Windows Phone toolkit (2)


VIVEK MASKARA

I am pursuing BTech in Software Engineering from Delhi Technological University. I  Hub tiles (2)

develop apps, create websites and blog about my app experiences.

 Windows Phone 8.1 (18)

 Azure (1)

0 Comments Windows App Tutorials 


1 Login  General (3)

 Recommend t Tweet f Share Sort by Best  Launchers (1)

 ListView (2)
Start the discussion…
 Location (1)
LOG IN WITH
OR SIGN UP WITH DISQUS ?
 Media (1)
Name

 SQLite Database (3)

 Syncfusion (3)

Be the first to comment.  UI (3)

 WPF (8)
ALSO ON WINDOWS APP TUTORIALS

Pass data from User Control to Parent page in Using Chromium Web Browser Control in WPF
Windows Phone 8.1 app
1 comment • 3 years ago 2 comments • 3 years ago NEVER MISS OUT ON NEW TUTORIALS!
Lexi Mize — Your step:Step 5: Subscribe to the Poornima — Hey I am not able to use the cefSharp
event in User Controlshould read:Step 5: Subscribe in my Xaml file. I get a error as{"Could not load file
Windows …
to the User Control event in the Parent Page or assembly 'CefSharp.Core, Version=63.0.3.0, …
615 likes

Write DataGrid to CSV file in WPF app How to Display Local Toast in Universal
1 comment • 3 years ago Windows Apps
Charlie — Datagrids are recognized, but the 1 comment • 3 years ago
Like Page
AvatarprepareData() method isn't recognized for me. Is Pedro Grijalva — The tutorial is incomplete. When
there a reference or namespace I'm not including? Avatarbuilding the project returns this error The
"CreatePriFilesForPortableLibraries" task was …
MORE ADVERTISMENTS
✉ Subscribe d Add Disqus to your site 🔒 Disqus' Privacy Policy

 Get Week Number of Month in C# Universal apps TextBlockButtonStyle 

GoJS HTML5 Diagram Library


GoJS is a fast and powerfulJavaScript library
for diagrams.
GoJS

AD RECENT POSTS TUTS HEAP

C# Base64 String to PNG Image

How to Hide or Show Console Windows from WPF App

How to Zoom Image in WPF app

Write DataGrid to CSV le in WPF app

GoJS HTML5 Diagram Library Call C-Sharp from javaScript using CefSharp in WPF
app
GoJS is a fast and powerfulJavaScript library
for diagrams.
GoJS

ARTICLE INDEX FEEDBACK WRITE FOR US Windows App Tutorials All rights reserved. Theme by Colorlib Powered by WordPress

You might also like