text stringlengths 0 13.4k |
|---|
Section 4.5: IEnumerable to Dictionary (≥ .NET 3.5) |
Create a Dictionary<TKey, TValue> from an IEnumerable<T>: |
using System; |
using System.Collections.Generic; |
using System.Linq; |
public class Fruits |
{ |
public int Id { get; set; } |
public string Name { get; set; } |
} |
var fruits = new[] |
{ |
new Fruits { Id = 8 , Name = "Apple" }, |
new Fruits { Id = 3 , Name = "Banana" }, |
new Fruits { Id = 7 , Name = "Mango" }, |
}; |
// Dictionary<int, string> key value |
var dictionary = fruits.ToDictionary(x => x.Id, x => x.Name); |
Section 4.6: Enumerating a Dictionary |
You can enumerate through a Dictionary in one of 3 ways: |
Using KeyValue pairs |
GoalKicker.com – .NET Framework Notes for Professionals |
14 |
Dictionary<int, string> dict = new Dictionary<int, string>(); |
foreach(KeyValuePair<int, string> kvp in dict) |
{ |
Console.WriteLine("Key : " + kvp.Key.ToString() + ", Value : " + kvp.Value); |
} |
Using Keys |
Dictionary<int, string> dict = new Dictionary<int, string>(); |
foreach(int key in dict.Keys) |
{ |
Console.WriteLine("Key : " + key.ToString() + ", Value : " + dict[key]); |
} |
Using Values |
Dictionary<int, string> dict = new Dictionary<int, string>(); |
foreach(string s in dict.Values) |
{ |
Console.WriteLine("Value : " + s); |
} |
Section 4.7: ConcurrentDictionary<TKey, TValue> (from .NET |
4.0) |
Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads |
concurrently. |
Creating an instance |
Creating an instance works pretty much the same way as with Dictionary<TKey, TValue>, e.g.: |
var dict = new ConcurrentDictionary<int, string>(); |
Adding or Updating |
You might be surprised, that there is no Add method, but instead there is AddOrUpdate with 2 overloads: |
(1) AddOrUpdate(TKey key, TValue, Func<TKey, TValue, TValue> addValue) - Adds a key/value pair if the key does |
not already exist, or updates a key/value pair by using the specified function if the key already exists. |
(2) AddOrUpdate(TKey key, Func<TKey, TValue> addValue, Func<TKey, TValue, TValue> updateValueFactory) |
- Uses the specified functions to add a key/value pair to the if the key does not already exist, or to update a key/value pair |
if the key already exists. |
Adding or updating a value, no matter what was the value if it was already present for given key (1): |
string addedValue = dict.AddOrUpdate(1, "First", (updateKey, valueOld) => "First"); |
Adding or updating a value, but now altering the value in update, based on the previous value (1): |
string addedValue2 = dict.AddOrUpdate(1, "First", (updateKey, valueOld) => $"{valueOld} Updated"); |
GoalKicker.com – .NET Framework Notes for Professionals |
15 |
Using the overload (2) we can also add new value using a factory: |
string addedValue3 = dict.AddOrUpdate(1, (key) => key == 1 ? "First" : "Not First", (updateKey, |
valueOld) => $"{valueOld} Updated"); |
Getting value |
Getting a value is the same as with the Dictionary<TKey,TValue>: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.