CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
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 );
if( position == -1 )
throw new ArgumentException( "No match.", "key" );
position++;
if( position >= dictionary.Count )
position = 0;
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 );
if( position == -1 )
throw new ArgumentException( "No match.", "value" );
position++;
if( position >= dictionary.Count )
position = 0;
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 );
if( position == -1 )
throw new ArgumentException( "No match.", "key" );
position--;
if( position < 0 )
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 );
if( position == -1 )
throw new ArgumentException( "No match.", "value" );
position--;
if( position < 0 )
position = dictionary.Count - 1;
K k = dictionary.Keys.ToList()[ position ];
V v = dictionary.Values.ToList()[ position ];
return new KeyValuePair<K, V>( k, v );
}
}
public class Program
{
public static void Main()
{
Dictionary<String, Int64>
dictionary = new Dictionary<String, Int64>() {
{ "1000L", 1000L },
{ "2000L", 2000L },
{ "3000L", 3000L },
{ "4000L", 4000L },
{ "5000L", 5000L },
{ "6000L", 6000L },
};
Random
random = new Random();
while( true ) {
Int64
value = random.Next( 1, 7 ) * 1000L;
Int32
position = dictionary.Values.ToList().IndexOf( value );
String
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();
Console.WriteLine( "Next Value: " + after.Value );
Console.WriteLine( "Next Key: " + after.Key );
Console.WriteLine();
Console.WriteLine( "Previous Value: " + before.Value );
Console.WriteLine( "Previous Key: " + before.Key );
Console.ReadLine();
Console.Clear();
}
}
}