Blog Home  Home Feed your aggregator (RSS 2.0)  
artiso Blog - Matrizenoperationen mit Extension Methods
Neues rund um's Thema .Net
 
 Wednesday, January 30, 2008

Mit Hilfe der neuen Extension Methods in C# 3.0 können einfache Matrizenoperationen ganz einfach als Methoden auf bestehende Objekttypen eingefügt werden. Ich habe hier mal ein Beispiel mit SortedDictionaries. Diese Extension Method erlaubt das addieren zweier Matrizen.

public static void AddDictionary(this SortedDictionary<int, decimal> resultDictionary, SortedDictionary<int, decimal> insertDictionary)
{
    foreach (int key in insertDictionary.Keys)
    {
        if (!resultDictionary.ContainsKey(key))
            resultDictionary.Add(key, insertDictionary[key]);
        else
            resultDictionary[key] += insertDictionary[key];
    }
}

 

Der Aufruf gestaltet sich dann sehr einfach:

SortedDictionary<int, decimal> a = new SortedDictionary();
SortedDictionary<int, decimal> b = new SortedDictionary();

//Werte in Dictionaries einfügen

a.AddDictionary(b);

 

Ich finde die Extension Methods ne feine Sache. Natürlich sind auch beliebige andere Operationen möglich.

Wednesday, January 30, 2008 5:47:30 PM (Mitteleuropäische Zeit, UTC+01:00)  #    Comments [2]    | 
Copyright © 2008 Thomas. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: