Preparing to MS Exam 70-483 - Implementation of common interfaces

The full set of questions I'm trying to cover in time of my preparation to MS Exam 70-483  you can find here.

Here is short overview of common interfaces and their methods.


IComparable, IComparer 

  • IComparable.CompareTo(object obj) defines a generalized type-specific comparison method that a value type or class implements to order or sort its instances.
  • IComparor.Compare(object obj1, object obj2) exposes a method that compares two objects.

IClonable

The ICloneable interface enables us to implement a mechanism for creation of a copy of an existing object. The ICloneable interface contains one member, the Clone method, which is intended to provide cloning support beyond that supplied by Object.MemberwiseClone.

IDisposable

The primary use of this interface is to release unmanaged resources. The GC automatically releases the memory allocated to a managed object when that object is no longer used. However, we still cannot predict when garbage collection will occur.

When calling a class implementing the IDisposable interface, we should use the try/finally pattern to make sure that unmanaged resources are disposed of even if an exception interrupts your application.

NOTE 
There is the using statement instead of the try/finally pattern.

IEnumerable, IEnumerator

  • IEnumerable.GetEnumerator() - returns an enumerator that iterates through a collection.
  • IEnumerator - supports a simple iteration over a nongeneric collection.
    • Current - gets the current element in the collection.
    • MoveNext() - moves the enumerator to the next element of the collection.
    • Reset() - reset the enumerator to its initial position (before the 1st element)
NOTE
Enumerators cannot be used to modify the underlying collection. Initially, the enumerator is positioned before the first element in the collection, so at this position, calling the Current property throws an exception.

If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false, so at this point calling Current throws an exception.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException. If the collection is modified between MoveNext and Current, Current returns the element that it is set to, even if the enumerator is already invalidated.

The enumerator does not have exclusive access to the collection; so, enumerating is not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

IEquatable

Defines a type-specific method for determining equality of instances.

The IEquatable interface is used by generic collection objects such as Dictionary, List, and LinkedList when testing for equality in such methods as Contains, IndexOf, LastIndexOf, and Remove. It should be implemented for any object that might be stored in a generic collection.


NOTE 

If you implement IEquatable, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable.Equals method. If you do override Object.Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. This ensures that all invocations of the Equals method return consistent results.

IQueryable

....

IUnknown

....

Comments