hrs
hrs

Reputation:

SortedDictionary (C#)- change value

In a SortedDictionary is it possible to change the value of an item ?

Upvotes: 4

Views: 7081

Answers (3)

Peter
Peter

Reputation: 2260

you can change values and keys, by definition keys are sorted (when adding)

To change a key, store value in a temp, remove the key, and Add the new key with the temp value.

Upvotes: 1

W. Kevin Hazzard
W. Kevin Hazzard

Reputation: 868

Yes, just use the indexer like this (in C#):

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        SortedDictionary<int, string> map = 
            new SortedDictionary<int, string>();
        map[1] = "Kevin Hazzard";
        Console.WriteLine( map[1] );
        map[1] = "W. Kevin Hazzard";
        Console.WriteLine( map[1] );
        Console.ReadLine();
    }
}

Upvotes: 3

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422016

Yes, why not?

sortedDictionary[key] = newValue;

Upvotes: 12

Related Questions