Skip to content

Instantly share code, notes, and snippets.

View Larry57's full-sized avatar

L4rry Larry57

View GitHub Profile
/// Source: http://stackoverflow.com/questions/268321/bidirectional-1-to-1-dictionary-in-c-sharp
/// <summary>
/// This is a dictionary guaranteed to have only one of each value and key.
/// It may be searched either by TFirst or by TSecond, giving a unique answer because it is 1 to 1.
/// </summary>
/// <typeparam name="TFirst">The type of the "key"</typeparam>
/// <typeparam name="TSecond">The type of the "value"</typeparam>
using System.Collections.Generic;
using System;
@Larry57
Larry57 / BinarySearch.cs
Created April 16, 2014 20:57
BinarySearch in a list, using a selector
namespace System.Collections.Generic
{
public static class ListExtensions
{
/// <summary>
/// Do a binary search in a generic list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
/// <param name="tf">list</param>
@Larry57
Larry57 / DictionaryExtension.cs
Created October 22, 2014 08:27
Process an element in a Dictionary only if it exists
namespace System.Collections.Generic
{
public static class DictionaryExtension
{
public static void ProcessValue<TKey, TValue>(this IDictionary<TKey, TValue> x, TKey key, Action<TValue> action)
{
TValue o;
if (x.TryGetValue(key, out o))
action(o);
}
@Larry57
Larry57 / EventArgsGeneric.cs
Created June 3, 2011 13:33
Generic version of EventArgs
using System;
namespace iTracker.UI.Forms.Tools
{
public class EventArgs<T> : EventArgs
{
public T Content
{
get;
set;
@Larry57
Larry57 / treehelper.cs
Created June 3, 2011 13:39
Ancestors and Descendants for Tree handling
using System;
using System.Collections.Generic;
public static class Extensions
{
static public IEnumerable<T> Descendants<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> descendBy)
{
foreach (T value in source)
{
yield return value;
@Larry57
Larry57 / DbHelper.cs
Created June 6, 2011 07:51
Minimal Db Helper
namespace DbHelper
{
public class OleDbHelper : DbHelper<OleDbConnection>
{
public static DbParameter CreateParameter(string name, object value)
{
return new OleDbParameter(name, value);
}
}
@Larry57
Larry57 / StringTruncate.cs
Created October 18, 2012 06:26
Truncate a string too long for display. So, "A too long string" become "A too long s..." if limit is 12 chars.
public static string Truncate(this string myString, int limit, string symbol)
{
if (myString == null)
return null;
if (limit < 0)
throw new ArgumentOutOfRangeException("limit", limit, "must be 0 or greater");
if (symbol == null)
throw new ArgumentNullException("symbol must not be null");
@Larry57
Larry57 / DuckTryParse.cs
Last active October 11, 2015 19:48
Universal TryParse
public class DuckTryParse
{
public static bool TryParse<duck>(string stringToParse, out duck value)
{
var method = MethodCache<duck>.TryParse;
if (method == null || !method(stringToParse, out value))
{
value = default(duck);
return false;
@Larry57
Larry57 / DesignMode.cs
Created October 18, 2012 06:36
Unflawed Design Mode
new bool DesignMode 
{
get
{
return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
}
}
@Larry57
Larry57 / WindowsFormsHost.xaml
Created October 18, 2012 06:37
Magic xaml tip to make a WindowsFormsHost resize correctly when the application is embedded in a XBAP.
<WindowsFormsHost Margin="0" x:Name="winFormHost" FontSize="11.33" FontFamily="Microsoft Sans Serif" />