Skip to content

Instantly share code, notes, and snippets.

@mabster
Created June 1, 2012 11:11
Show Gist options
  • Save mabster/2851282 to your computer and use it in GitHub Desktop.
Save mabster/2851282 to your computer and use it in GitHub Desktop.
IComparable with Fody?
// before
public class Foo : IComparable<Foo>
{
int Id { get; set; }
public int CompareTo(Foo other)
{
if (other == null) return -1;
return this.Id.CompareTo(other.Id);
}
}
// after
public class Foo : IComparable<Foo>, IComparable
{
int Id { get; set; }
public int CompareTo(Foo other)
{
if (other == null) return 1;
return this.Id.CompareTo(other.Id);
}
public int CompareTo(object obj)
{
if (obj == null) return 1;
var foo = obj as Foo;
if (foo == null) throw new ArgumentException("Object is not of type " + this.GetType());
return this.CompareTo(foo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment