IDictionary Remove All by condition - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

IDictionary Remove All by condition

Demo Code

/*//from  ww w. j ava 2  s.  c om
 Copyright 2014 - 2015 Nikita Bernthaler
 DictionaryExtensions.cs is part of SFXLibrary.

 SFXLibrary is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 SFXLibrary is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with SFXLibrary. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        /// <exception cref="Exception">A delegate callback throws an exception.</exception>
        public static void RemoveAll<TK, TV>(this IDictionary<TK, TV> dict, Func<TK, TV, bool> match)
        {
            foreach (var key in dict.Keys.ToArray().Where(key => match(key, dict[key])))
            {
                dict.Remove(key);
            }
        }
}

Related Tutorials