Friday, August 3, 2018

Equality in C#

  • o1.Equals(o2)     - equality
    • By default, true if o1 and o2 are created by execution of the same new
    • Can be redefined in a particular class
  • Object.ReferenceEquals(o1, o2)     - identity
    • True if both o1 and o2 are null, or if they are created by execution of the same new
    • Static - cannot be redefined.
  • Object.Equals(o1, o2)
    • True if Object.ReferenceEquals(o1, o2), or if o1.Equals(o2)
  • o1 == o2
    • True if both o1 and o2 are null, or if they are created by execution of the same new
    • An overloadable operator

  • Source
  • Thursday, August 2, 2018

    LINQ complexity

     
    There are very, very few guarantees, but there are a few optimizations:
    • Extension methods that use indexed access, such as ElementAt, Skip, Last or LastOrDefault, will check to see whether or not the underlying type implements IList<T>, so that you get O(1) access instead of O(N).
    • The Count method checks for an ICollection implementation, so that this operation is O(1) instead of O(N).
    • Distinct, GroupBy Join, and I believe also the set-aggregation methods (Union, Intersect and Except) use hashing, so they should be close to O(N) instead of O(N²).
    • Contains checks for an ICollection implementation, so it may be O(1) if the underlying collection is also O(1), such as a HashSet<T>, but this is depends on the actual data structure and is not guaranteed. Hash sets override the Contains method, that's why they are O(1).
    • OrderBy methods use a stable quicksort, so they're O(N log N) average case.
    I think that covers most if not all of the built-in extension methods. There really are very few performance guarantees; Linq itself will try to take advantage of efficient data structures but it isn't a free pass to write potentially inefficient code.