(How)
C^# You Are - 30 December 2007
.NET is full of cases where similarly sounding names do different things. This week's questions involve names that sound similar but aren't. Some of these questions may have appeared in the past.
- What is the difference between IComparer<T> and IEqualityComparer<T>? Answer
The IComparer<T> interface is used for comparing objects for ordering purposes. Objects compare as less than, equal to or greater than.
The IEqualityComparer<T> interface is used for equality comparison. Equality comparison is only interested in whether two objects are equal or not.
- What is the difference between IComparer<T> and IComparable<T>? Answer
The IComparer<T>-style interfaces are defined two compare two objects. They are not necessarily implemented on the objects themselves. The
IComparable<T> interface is defined on an object and is used to compare the object to another object. As with IComparer it does comparison
for ordering purposes.
As an additional difference, most classes that must do comparison of objects accept an IComparer implementation as to allow comparison without
requiring that an actual object exist.
- What is the difference between StringComparer and StringComparison? Answer
StringComparer is a static class that implements IComparer<T> and is used to compare string values for ordering. There are various
properties on the class that provide different comparison algorithms such as case sensitive and case insensitive.
StringComparison is an enumerated type that can be passed to certain comparison routines. This enumeration describes the type of
comparison to be performed. It is not an concidence that the enumerated values match the exposed properties of StringComparer.
- What is the difference between ICollection and IList? Answer
ICollection is all but useless in .NET for any concrete programming. It simply allows for copying the collection elements to an array. You cannot even access the elements through this interface. The best
you can do is enumerate the elements using the IEnumerable interface.
IList extends ICollection and adds support for retrieving and setting items at a given index.
Traditionally you would use ICollection if you wanted to be absolutely generic in the type of collection you worked with (stack, list, dictionary, etc) whereas you would
use IList when you were working with array-style collections.
- What is the difference between ICollection<T> and IList<T>? Answer
Now things start to get confusing. The generic collection interface is still the base interface for collections. It does support adding and removing individual elements
but it does not define any relation as to how they are added.
The IList<T> interface, again, derives from the base generic collection interface and adds support for accessing the elements and adding/removing them via index.
As with the non-generic versions, you will generally use the generic list interface for interacting with collections and reserve the generic collection interface for when
you have no idea what type of collection you're working with.
- What is the difference between IEnumerable<T> and IEnumerator<T>? Answer
In an attempt to completely generalize all functionality, .NET differentiates between enumerating a collection and objects that can be enumerated. The
IEnumerable<T> interface is used to identify an object that can be enumerated. It's single method gets the enumerator to be used. All collections
and some non-collections implement this interface.
The IEnumerator<T> interface is used to enumerate an object/collection. How the enumeration is done is up to the implementation. You can, if you like,
expose multiple enumerators (all deriving from IEnumerator<T>) to allow enumeration in a forward, reverse or even random order. The enumerator is
responsible for keeping tracking of where you are at and moving to the next element. Most enumerators will generate an exception if the underlying object being enumerated
changes. This is because it is easier than trying to keep the enumerator in sync with the original object and faster than copying the original data.
- What is the difference between IFormatter, IFormattable and IFormatProvider? Answer
Unlike the enumerator/enumerable interfaces, these two interfaces are not related at all.
The IFormatter interface is used to serialize and deserialize an object to some format. The format is defined by the formatter. .NET ships
with support for binary and XML.
The IFormattable interface is used to convert an object to a string.
The IFormatProvider is used to retrieve an object that can format another object to a string. The returned object would implement the ICustomFormatter interface
to allow for custom formatting of an object to a string. String.Format accepts an IFormatProvider as a parameter. You can define custom formatters to format
objects in a more suitable format. For example the Kraken library provides SizeFormatter which implements IFormatProvider and ICustomFormatter to allow
for converting numeric values to formatted sizes (like megabytes or kilobytes).
- What is the difference between Control and UserControl? Answer
Control is the base class for WinForms-controls (including forms). It provides basic window management support and is used to create new controls that
are not already available in .NET. When creating a new control that mimics an existing control you would normally use the existing control as a base.
Oftentimes a control is nothing more than a composition of other controls. In this case UserControl is a better option. It is a control that handles the
basic functionality of all controls including positioning. It extends the base control class.
- What is the difference between IHttpModule and IHttpHandler? Answer
IHttpModule identifies a module to IIS. IHttpHandler identifies a handler. An HTTP module is used to extend the functionality of IIS with
new features such as encryption, compression and/or security. Most of the ASP.NET functionality is added through HTTP modules. Modules are called on
every page request and can add, modify or remove the request and response information. For example a security module might fail any requests to certain
pages whereas an encryption module might decrypt requests coming in and encrypt requests going out.
An HTTP handler, on the other hand, is responsible for handling an entire request. The default ASP.NET page handler is responsible for processing page requests
but occasionally you want to do custom handling for certain file types. Web resources is a good example. In .NET a new handler was added for resources such that
requests for resources come into IIS as would any normal request. Rather than the page handler dealing with the request the resource handler picks up the request,
retrieves the appropriate resource and then sends it back to the client.
- What is the difference between Process and ProcessModule? Answer
Both classes represent similar things but they serve different purposes. Every binary (DLL or EXE) loaded into a process has a module associated with it.
The module identifies where it was loaded, the path to the binary and other binary-specific information. This is most useful for diagnostic purposes.
The Process class is used to represent a process running in Windows. Each process has only a single instance. The Process object provides
process-level information such as the main window, running threads and startup information. Each EXE, therefore, has both a Process object and a
ProcessModule representing it while a DLL has only a ProcessModule.