Lokesh Darapureddy: C# Dictionary Two Remove Methods            
       
                                                               

Sunday, 30 June 2024

                               
                                                                   

                            C# Dictionary Two Remove Methods                        

                                                               
                       
C# Dictionary Remove(TKey) vs Remove(TKey, TValue)

Remove(TKey key)

Removes the entry from the dictionary with the specified key.

            
function removeKey() {
    var dictionary = {
        1: "One",
        2: "Two",
        3: "Three"
    };

    // Removing entry with key 2
    if (dictionary.hasOwnProperty(2)) {
        delete dictionary[2];
        console.log("Entry with key 2 removed successfully");
        console.log(dictionary);
    } else {
        console.log("Key 2 not found in dictionary");
    }
}
            
        

Remove(TKey key, out TValue value)

Removes the entry from the dictionary with the specified key and retrieves the associated value.

            
function removeKeyAndValue() {
    var dictionary = {
        1: "One",
        2: "Two",
        3: "Three"
    };

    var removedValue;
    // Removing entry with key 3 and retrieving the removed value
    if (dictionary.hasOwnProperty(3)) {
        removedValue = dictionary[3];
        delete dictionary[3];
        console.log("Entry with key 3 ('" + removedValue + "') removed successfully");
        console.log(dictionary);
    } else {
        console.log("Key 3 not found in dictionary");
    }
}
            
        
                   
                                   
               
                                           

0 Comments:

                       
                                                                                                           
                       

                           

Post a Comment

                       

                       

Subscribe to Post Comments [Atom]

                                       

                        << Home