АкушерствоАнатомияАнестезиологияВакцинопрофилактикаВалеологияВетеринарияГигиенаЗаболеванияИммунологияКардиологияНеврологияНефрологияОнкологияОториноларингологияОфтальмологияПаразитологияПедиатрияПервая помощьПсихиатрияПульмонологияРеанимацияРевматологияСтоматологияТерапияТоксикологияТравматологияУрологияФармакологияФармацевтикаФизиотерапияФтизиатрияХирургияЭндокринологияЭпидемиология

Dictionaries in C#

Dictionaries represent a sophisticated data structure that allows you to access an element based on a key. In other words, A Dictionary class represents a dictionary in C# that is used to represent a collection of keys and values pair of data. This article demonstrates how to use a dictionary in C#.. Dictionaries are also known as hash tables or maps. The main feature of dictionaries is fast lookup based on keys. You can also add and remove items freely, a bit like a List<T>, but without the performance overhead of having to shift subsequent items in memory.

 

A good performance of the dictionary is based on a good implementation of the method GetHashCode().

 

SortedDictionary < TKey, TValue >is a binary search tree where the items are sorted based on the key. The key type must implement the interface IComparable < TKey >. If the key type is not sortable, you can also create a comparer implementing IComparer < TKey >and assign the comparer as a constructor argument of the sorted dictionary.

TKey - The type of the keys in the dictionary.

TValue - The type of the values in the dictionary.

 

using System; using System.Collections.Generic; class Program{ static void Main() { Example e = new Example(); Console.WriteLine(e.GetValue()); }} class Example{ Dictionary<int, int> _d = new Dictionary<int, int>() { {1, 1},{2, 3}, {3, 5}, {6, 10} }; public int GetValue() { return _d[2]; // Example only } }Output: 3

Дата добавления: 2015-09-18 | Просмотры: 473 | Нарушение авторских прав



1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |



При использовании материала ссылка на сайт medlec.org обязательна! (0.004 сек.)