Skip to content

An extension method that combines the removal of a key from an IDictionary and post-removal processing of that key's associated value into one method call. A part of the C# Language Syntactic Sugar suite.

License

Notifications You must be signed in to change notification settings

tonygiang/CLSS.ExtensionMethods.IDictionary.RemoveAndProcess

Repository files navigation

CLSS.ExtensionMethods.IDictionary.RemoveAndProcess

Problem

It's common to find situations where removing an object from a collection means you also want to do something with it, such as disposing of it. In a barebone .NET application, you can count on the .NET Garbage Collector to clean up the removed object, but in many domains, disposing of an unused object requires an additional manual call, as the framework still keep references to it. These 2 operations are each done in a separate line of code and therefore not very friendly to a functional syntax.

var NumberSpellings = new Dictionary<int, string>();
Hide(NumberSpellings[4]);
NumberSpellings.Remove(4);

Solution

RemoveAndProcess is an extension method for all IDictionary<TKey, TValue> types that combines these 2 steps into 1 method.

using CLSS;

var NumberSpellings = new Dictionary<int, string>();
NumberSpellings.RemoveAndProcess(4, Hide);
// using a lambda expression
NumberSpellings.RemoveAndProcess(4, str => { PlayFadeAnimation(str); AddScore(str); });

RemoveAndProcess takes the value associated with the key specified by the first argument, removes the key from the source collection and passes that value to the delegate in the second argument.

Non-void methods are also accepted as the second argument.

using CLSS;

bool TryDispose(ResourceHandler handler) { ... };

var FileResourceMappings = new Dictionary<int, ResourceHandler>();
FileResourceMappings.RemoveAndProcess(4, TryDispose);

RemoveAndProcess returns the source IDictionary<TKey, TValue> to be friendly to a functional-style call chain. The exact return type will be determined by the invocation syntax of RemoveAndProcess. With an implicit type invocation, it returns an IDictionary<TKey, TValue>. With an explicit type invocation, it returns the original collection type.

using CLSS;

var NumberSpellings = new Dictionary<int, string>();
NumberSpellings.RemoveAndProcess(2, str => { ... }); // returns IDictionary<int, string>
NumberSpellings.RemoveAndProcess<Dictionary<int, string>, int, string>(2,
  str => { ... }); // returns Dictionary<int, string>
This package is a part of the C# Language Syntactic Sugar suite.

About

An extension method that combines the removal of a key from an IDictionary and post-removal processing of that key's associated value into one method call. A part of the C# Language Syntactic Sugar suite.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages