Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Concurrent;
- class ConcurrentDictionary
- {
- static void Main(string[] args)
- {
- ConcurrentDictionary<int, string> dict = new ConcurrentDictionary<int, string>();
- dict.TryAdd(1, "January"); //returns true
- dict.TryAdd(2, "February"); //returns true
- dict.TryAdd(3, "March"); //returns true
- dict.TryAdd(4, "Apr"); //returns true
- string updatedValue;
- bool returnTrue = dict.TryUpdate(4, "April", "Apr"); //Returns true
- dict.TryGetValue(4, out updatedValue);
- Console.WriteLine(updatedValue); //Display "April"
- bool returnsFalse = dict.TryUpdate(4, "Apr", "Apr"); //Returns false
- dict.TryGetValue(4, out updatedValue); //Returns "April" Old value
- Console.WriteLine(updatedValue); //Returns "April"
- }
- }
Add Comment
Please, Sign In to add comment