Skip to main content

Lindsay Donaghe's Library tagged generics   View Popular

20 May 08

Linq: The Missing ToDictionary Extension Method Overload - Omer van Kloeten's .NET Zen

An extension method for getting a generic dictionary back from a collection of KeyValuePairs... just a bit of a shortcut from using the overload with two params, but handy.

weblogs.asp.net/...6121417.aspx - Preview

generics C# .Net codesnippets

  • public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> enumeration)
    {
    // Check to see that enumeration is not null
    if (enumeration == null)
    throw new ArgumentNullException("enumeration");

    return enumeration.ToDictionary(item => item.Key, item => item.Value);
    }
14 May 08

Yield and generics rock! - Tales from the Evil Empire

How to create a filter for generic lists (or other IEnumerable objects) without creating a temporary list first. Illustrates how to use the yield statement as well and create your own generic enumerator.

weblogs.asp.net/...223531.aspx - Preview

C# .Net codesnippets development generics

  • public sealed class FilteredEnumerable<T> : IEnumerable<T>, IEnumerable {

      private IEnumerable<T> _enumerable;
      private Predicate<T> _filter;

      public FilteredEnumerable(IEnumerable<T> enumerable, Predicate<T> filter) : base() {
        _enumerable = enumerable;
        _filter = filter;
      }

      IEnumerator<T> IEnumerable<T>.GetEnumerator() {
        foreach (T item in _enumerable) {
          if (_filter == null || _filter(item)) {
            yield return item;
          }
        }
      }

      IEnumerator IEnumerable.GetEnumerator() {
        return (IEnumerator)(((IEnumerable<T>)this).GetEnumerator());
      }
    }
  • string[] stringsToFilter = new string[] {"Red", "Green", "Blue", "Pink"};
    Predicate<string> filter = delegate(string stringToFilter) {
      return (stringToFilter.IndexOf('e') != -1);
    };
    filteredStrings = new FilteredEnumerable<string>(stringsToFilter, filter);
14 Apr 08

Steven Smith : Render User Control as String Template

A generics variation of Scott Guthrie's code to render ASCX controls as HTML for Ajax consumption.

aspadvice.com/...ontrol-as-String-Template.aspx - Preview

asp.net web development codesnippets generics C#

1 - 3 of 3
Showing 20 items per page

Highlighter, Sticky notes, Tagging, Groups and Network: integrated suite dramatically boosting research productivity. Learn more »

Join Diigo