Lindsay Donaghe's Library tagged → 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.
-
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.
-
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.
1 - 3 of 3
Showing 20▼ items per page
Sponsored Links
Ads by Google
Top Contributors
Groups interested in generics
Related Lists on Diigo
-
Health
Items: 1 | Visits: 1
Created by: Alex Smith
Highlighter, Sticky notes, Tagging, Groups and Network: integrated suite dramatically boosting research productivity. Learn more »
Join Diigo
