dictionary c#
C#
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("apple", 1);
dictionary.Add("windows", 5);
// See whether Dictionary contains this string.
if (dictionary.ContainsKey("apple"))
{
int value = dictionary["apple"];
Console.WriteLine(value);
}
// See whether it contains this string.
if (!dictionary.ContainsKey("acorn"))
{
Console.WriteLine(false);
}
}
}IDictionary<int, string> dict = new Dictionary<int, string>();
//or
Dictionary<int, string> dict = new Dictionary<int, string>();
// 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 Add method throws an exception if the new key is
// already in the dictionary.
try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = \"rtf\", value = {0}.",
openWith["rtf"]);
// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.",
openWith["rtf"]);
// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
openWith["doc"] = "winword.exe";
// The indexer throws an exception if the requested key is
// not in the dictionary.
try
{
Console.WriteLine("For key = \"tif\", value = {0}.",
openWith["tif"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue can be a more efficient
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))
{
Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
Console.WriteLine("Key = \"tif\" is not found.");
}
// ContainsKey can be used to test keys before inserting
// them.
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}",
openWith["ht"]);
}
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
// To get the values alone, use the Values property.
Dictionary<string, string>.ValueCollection valueColl =
openWith.Values;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach( string s in valueColl )
{
Console.WriteLine("Value = {0}", s);
}
// To get the keys alone, use the Keys property.
Dictionary<string, string>.KeyCollection keyColl =
openWith.Keys;
// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console.WriteLine();
foreach( string s in keyColl )
{
Console.WriteLine("Key = {0}", s);
}
// Use the Remove method to remove a key/value pair.
Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}
/* This code example produces the following output:
An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe
Key = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe
Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe
Value = hypertrm.exe
Key = txt
Key = bmp
Key = dib
Key = rtf
Key = doc
Key = ht
Remove("doc")
Key "doc" is not found.
*/
// To add an item to a dictionary use 'Add()'
dict.Add(1,"One");
Also in C#:
- Title
- animator hash in unity
- Category
- C#
- Title
- windows forms check if form is disposed
- Category
- C#
- Title
- unity check if gameobject is active
- Category
- C#
- Title
- how to store some variables on the device in unity
- Category
- C#
- Title
- unity change the source image or image
- Category
- C#
- Title
- move file from one folder to another c#
- Category
- C#
- Title
- asp.net list all files in folder
- Category
- C#
- Title
- c# reverse list
- Category
- C#
- Title
- bold caption latex
- Category
- C#
- Title
- c# reflection create generic type
- Category
- C#
- Title
- unity array of child objects
- Category
- C#
- Title
- c# how to refreshyour bindingsource
- Category
- C#
- Title
- .net core download image from url binary file
- Category
- C#
- Title
- create dropdown in datatable c# dynamically
- Category
- C#
- Title
- C# get all files in directory
- Category
- C#
- Title
- get hash c#
- Category
- C#
- Title
- c# replace string case insensitive
- Category
- C#
- Title
- if and c#
- Category
- C#
- Title
- unity instantiate
- Category
- C#
- Title
- c# array isn't working
- Category
- C#
- Title
- how to stop a form c#
- Category
- C#
- Title
- copy a list C#
- Category
- C#
- Title
- repeater itemdatabound event in c#
- Category
- C#
- Title
- create char array c#
- Category
- C#
- Title
- decalre an int list mvc
- Category
- C#
- Title
- c# yield
- Category
- C#
- Title
- c# multiline comment
- Category
- C#
- Title
- C# .net core convert int to enum
- Category
- C#
- Title
- C# get pc language
- Category
- C#
- Title
- get number of sundays in a month c#
- Category
- C#
- Title
- how to create a list in c# unity
- Category
- C#
- Title
- landscape print gridcontrol devexpress
- Category
- C#
- Title
- add row and columns to grid wpf in code
- Category
- C#
- Title
- how to get object to spawn in a curcle
- Category
- C#
- Title
- What is a class in c#
- Category
- C#
- Title
- C# colours
- Category
- C#
- Title
- change textbox location C#
- Category
- C#
- Title
- Assets/Scripts/Snake.cs(177,25): error CS1061: Type `Snake.SnakeBodyPart' does not contain a definition for `SetGridPostion' and no extension method `SetGridPostion' of type `Snake.SnakeBodyPart' could be found. Are you missing an assembly reference?
- Category
- C#
- Title
- .net core session
- Category
- C#
- Title
- c# format string to date yyyymmdd
- Category
- C#
- Title
- c# print expression tree
- Category
- C#
- Title
- c# reverse a string
- Category
- C#
- Title
- .sh script: check if file exist
- Category
- C#
- Title
- how to turn a string in a char list c#
- Category
- C#
- Title
- unity load scene
- Category
- C#
- Title
- c# keyvaluepair
- Category
- C#
- Title
- to list c#
- Category
- C#
- Title
- new command - latex
- Category
- C#
- Title
- c# if statement
- Category
- C#
- Title
- c sharp if string equals
- Category
- C#
- Title
- c# polymorphism
- Category
- C#
- Title
- Assets/Scripts/Snake.cs(187,10): error CS0029: Cannot implicitly convert type `UnityEngine.Vector2Int' to `System.Collections.Generic.List<UnityEngine.Vector2Int>'
- Category
- C#
- Title
- center an image horizontally and vertically
- Category
- C#
- Title
- https request c#
- Category
- C#
- Title
- c# object clone
- Category
- C#
- Title
- c# list to string
- Category
- C#
- Title
- c# combobox datasource enum
- Category
- C#
- Title
- c# get the last item in a list
- Category
- C#
- Title
- F# convert generic.List to list
- Category
- C#
- Title
- webutility.urlencode space
- Category
- C#
- Title
- asp.net textarea disable resize
- Category
- C#
- Title
- How to get the world position of the edge of an object?
- Category
- C#
- Title
- convert string to boolean c#
- Category
- C#
- Title
- c# razor add disabled to button if
- Category
- C#
- Title
- why is c# say ; expected
- Category
- C#
- Title
- mouseposition unity
- Category
- C#
- Title
- c# find process by name
- Category
- C#
- Title
- Unity C# make object face away
- Category
- C#
- Title
- c# filter non alphanumeric characters
- Category
- C#
- Title
- first sentence letter capital in c#
- Category
- C#
- Title
- vb.net tostring numeric format string
- Category
- C#
- Title
- unity assign button onclick
- Category
- C#
- Title
- how to route back to url using Request.Headers["Referer"].ToString() in asp.net core
- Category
- C#
- Title
- unity move character
- Category
- C#
- Title
- trello
- Category
- C#
- Title
- c# read all text from a file
- Category
- C#
- Title
- unity how to check object position
- Category
- C#
- Title
- modal dismiss
- Category
- C#
- Title
- c# calculate difference between two dates in days
- Category
- C#
- Title
- split string in c#
- Category
- C#
- Title
- unity load next scene
- Category
- C#
- Title
- unity string format time
- Category
- C#
- Title
- c# list tuple
- Category
- C#
- Title
- c# object list attribute to string
- Category
- C#
- Title
- uwp file open picker
- Category
- C#
- Title
- wpf make size fill all grid
- Category
- C#
- Title
- csharp attributes as generics constraints
- Category
- C#
- Title
- ASP select box all states
- Category
- C#
- Title
- unity3d find y position on navmesh
- Category
- C#
- Title
- query associative table ef6
- Category
- C#
- Title
- unity how to change max fps
- Category
- C#
- Title
- what does thismean incsharp public static void Main
- Category
- C#
- Title
- split string
- Category
- C#
- Title
- asp net identity include phone number when registering
- Category
- C#
- Title
- how to update modal class using dbfirst in asp.net core
- Category
- C#
- Title
- Read csv file into wpf C#
- Category
- C#
- Title
- c# string array to string
- Category
- C#
- Title
- inheritance c#
- Category
- C#
- Title
- generate a dropdown list from array data using razor .net mvc
- Category
- C#
- Title
- c# append to file
- Category
- C#