From Schmid.wiki
Jump to: navigation, search

Sorting:

List<Thing> things;
things.Sort(delegate(Thing t1, Thing t2)
  { return t1.value.CompareTo(t2.value); }
);
// CompareTo <  0: this <  other
// CompareTo == 0: this == other
// CompareTo >  0: this >  other

Copying a list of value types (primitives, structs) by value:

struct Thing {}
List<Thing> things;
List<Thing> copyOfThings = List<Thing>(things);

Copying a list of reference types (classes) by value:

class Thing {
    int data;
    public Thing(Thing other) { // copy ctor
        this.data = other.data;
    }
}
// copy list and entries by value
List<Thing> things;
copyOfThings = new List<Thing>(things.Count);
foreach(Thing thing in things)
   copyOfThings.Add(new Thing(thing)); // use copy ctor