Generische Listen sortieren
Frank Dzaebel, erstellt am: 3.11.2005, zuletzt geändert:  27.5.2007
Kategorie: Generics, .NET-Version: 2.0

Unter .NET 2.0 gibt es als neues Feature die Generics. Wegen der besseren Performanz, und besseren Wartbarkeit und Typsicherheit sind sie in vielen Fällen den klassischen ArrayList'en oder von CollectionBase abgeleiteten Listen (.NET 1.1) vorzuziehen.
Siehe auch "Beispiel für Queue mit struct".
- MSDN : Generics FAQ: Fundamentals
- MSDN Magazine
January 2006
: Base Class Library Performance Tips and Tricks
Comparing Generic and Non-Generic Collections 
- Webcast : Generics mit C#
- MSDN : System.Collections.Generic-Namespace
- Dzaebel : List<T> Beispiel für C#
- MSDN : IComparer<T> , List<T>, IComparable<T>
- MSDN : Übersicht über Generika in .NET Framework
- MSDN Magazine : Introducing Generics in the CLR
- MSDN Blog : Design Guidelines Update: Generics
- IDesign J.L. : An Introduction to C# Generics
- MSDN Blog : Why we don’t recommend using List<T> in public APIs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace ListIComparer
{
  public partial class Form1 : Form
  {
    public Form1() {InitializeComponent();}

    private void Form1_Load(object sender,EventArgs e)
    {
      List<BaseReportItem> list = new List<BaseReportItem>();
      list.Add(new BaseReportItem(new Point(30,40)));
      list.Add(new BaseReportItem(new Point(50,60)));
      list.Add(new BaseReportItem(new Point(40,20)));
      list.Sort(); // <-- Hier Breakpoint setzen
    }
  }

  class BaseReportItem : IComparable<BaseReportItem>
  {
    public BaseReportItem(Point loc) {location = loc;}

    private Point location;
    public Point Location
    { get { return location;   }
      set { location = value; }
    }

    private LocationSorter locationSorter = new LocationSorter();
    public int CompareTo(BaseReportItem other)
    {
      return locationSorter.Compare(this,other);
    }
  }

  class LocationSorter : IComparer<BaseReportItem>
  {
    public int Compare(BaseReportItem left,BaseReportItem right)
    {
      if (left == null && right == null) return 0;
      if (left == null && right != null) return -1;
      if (right == null) return 1;
      if (left.Location.Y == right.Location.Y)
        return left.Location.X - right.Location.X;
      return left.Location.Y - right.Location.Y;
    }
  }
}


Beispiel für eine Queue mit einem struct
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace StructQueue
{
  public partial class Form1 : Form
  {
    public Form1() { InitializeComponent(); }

    private void Form1_Load(object sender,EventArgs e)
    {
      Queue<MeinStruct> queue = new Queue<MeinStruct>();
      queue.Enqueue(new MeinStruct(new Point(30,40)));
      queue.Enqueue(new MeinStruct(new Point(50,60)));
      queue.Enqueue(new MeinStruct(new Point(40,20)));
      MeinStruct current = queue.Peek();
      MessageBox.Show(current.Location.ToString()); //Ausgabe {X=30,Y=40}
    }
  }

  struct MeinStruct 
  {
    public MeinStruct(Point loc) { location = loc; }

    private Point location;
    public Point Location
    {
      get { return location; }
      set { location = value; }
    }
  }
}