using System.Collections.Generic;
public static class Extensions {
public static KeyValuePair<K, V> After<K, V>( this Dictionary<K, V> dictionary, K key ) {
Int32 position = dictionary.Keys.ToList().IndexOf( key );
throw new ArgumentException( "No match.", "key" );
if( position >= dictionary.Count )
K k = dictionary.Keys.ToList()[ position ];
V v = dictionary.Values.ToList()[ position ];
return new KeyValuePair<K, V>( k, v );
public static KeyValuePair<K, V> After<K, V>( this Dictionary<K, V> dictionary, V value ) {
Int32 position = dictionary.Values.ToList().IndexOf( value );
throw new ArgumentException( "No match.", "value" );
if( position >= dictionary.Count )
K k = dictionary.Keys.ToList()[ position ];
V v = dictionary.Values.ToList()[ position ];
return new KeyValuePair<K, V>( k, v );
public static KeyValuePair<K, V> Before<K, V>( this Dictionary<K, V> dictionary, K key ) {
Int32 position = dictionary.Keys.ToList().IndexOf( key );
throw new ArgumentException( "No match.", "key" );
position = dictionary.Count - 1;
K k = dictionary.Keys.ToList()[ position ];
V v = dictionary.Values.ToList()[ position ];
return new KeyValuePair<K, V>( k, v );
public static KeyValuePair<K, V> Before<K, V>( this Dictionary<K, V> dictionary, V value ) {
Int32 position = dictionary.Values.ToList().IndexOf( value );
throw new ArgumentException( "No match.", "value" );
position = dictionary.Count - 1;
K k = dictionary.Keys.ToList()[ position ];
V v = dictionary.Values.ToList()[ position ];
return new KeyValuePair<K, V>( k, v );
public static void Main()
Dictionary<String, Int64>
dictionary = new Dictionary<String, Int64>() {
value = random.Next( 1, 7 ) * 1000L;
position = dictionary.Values.ToList().IndexOf( value );
key = dictionary.Keys.ToList()[ position ];
KeyValuePair<String, Int64>
after = dictionary.After( value ),
before = dictionary.Before( value );
Console.WriteLine( "Value: " + value );
Console.WriteLine( "Key: " + key );
Console.WriteLine( "Next Value: " + after.Value );
Console.WriteLine( "Next Key: " + after.Key );
Console.WriteLine( "Previous Value: " + before.Value );
Console.WriteLine( "Previous Key: " + before.Key );