👨‍💻
wiki - owain.codes
  • 💻 Welcome
  • Coding
    • Tips / How-to
      • Where is the hosts file on Windows?
      • Wrapped up a site
    • Snippets
      • Non-coding
        • TinyMCE config generator for Umbraco
        • Git Markdown
        • Command Prompt / Terminal
      • 👨‍💻Coding
        • Compile FrontEnd via NVM and Gulp
        • Node-Sass
        • SQL Statements
        • Build up SQL statements
        • Get current logged in user from Controller
        • Dynamic BlockList Label
        • Alt Tag
    • Umbraco
      • Luke - Version helper
      • Umbraco 10+
        • Find out Model of Current page
        • error NU1301: Unable to load the service index for source
      • Umbraco 9
        • Get the current page content type alias
      • Fluent SQL
      • Unable to open ConfigSource file
      • Delete items from backoffice by DocType ID
      • [WIP] Setting up Examine
      • Umbraco.ModelsBuilder assembly error
      • Working with IPublishedContent
      • How to Strongly Type to Models
      • Setting up a custom form
      • Getting bounced away from /umbraco
      • Examine
        • Explaining GroupedOr / GroupedAnd methods
      • Rosyln error
      • Models Builder Settings
      • Adding content to the backoffice
      • Pagination
      • Creating YYYY/MM folders
      • Configuration Error - CodeDom
    • C Sharp
      • Useful Links
      • Regex
      • Null check .any()
      • internal static and internal const
      • Dependency Injection
      • Dictionary<Tkey, TValue>
      • Linq / Lambda
  • Git
    • Git Actions
      • Build your own Git Action
      • Create a readme file automatically
    • Command line
      • Unstage a file
      • Ignore web.config changes on commit
      • Allow for case-insensitivity in Windows
      • Making VSCode your Git editor and diff tool
      • Add, Push, Pull, Clone
      • Make Terminal look nice
    • Conventional Commit
Powered by GitBook
On this page

Was this helpful?

  1. Coding
  2. C Sharp

Dictionary<Tkey, TValue>

Represents a collection of keys and values.

PreviousDependency InjectionNextLinq / Lambda

Last updated 4 years ago

Was this helpful?

Added 04/11/2020 Working with dictionaries for adding a filter value to the backoffice of Umbraco on CAS.

TKey - The type of the keys in the dictionary.

TValue - The type of the values in the dictionary.

// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

The generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the class is implemented as a hash table.

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		Dictionary<string, string> openWith = new Dictionary<string, string>();
		// Add some elements to the dictionary. There are no
		// duplicate keys, but some of the values are duplicates.
		openWith.Add("txt", "notepad.exe");
		openWith.Add("bmp", "paint.exe");
		openWith.Add("dib", "paint.exe");
		openWith.Add("rtf", "wordpad.exe");
		
		foreach (KeyValuePair<string, string> kvp in openWith)
		{
			Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
		}
	}
}
Dictionary<TKey,TValue>
Dictionary<TKey,TValue>