System.ComponentModel.IContainer components = null;
Yet others have
System.ComponentModel.Container components = null;
What is the difference of this variable being a reference to the
Container class as opposed to the Interface?
The first declaration can contain a reference to any object that
implements the IContainer interface. The second can point only to a
System.ComponentModel.Container class, or a class that inherits from
System.ComponentModel.Container.
You can easily cast them:
System.ComponentModel.Component ob = new Component();
System.ComponentModel.IComponent inter;
inter = (System.ComponentModel.IComponent) ob;
ob = (System.ComponentModel.Component) inter;
The cast from Component to IComponent should always succeed. Casting from
IComponent to Component success only when the former is a reference to a
Component, not to another class that implements the IComponent interface.
Here's some sample code to demonstrate the difference:
class NotAComponent : IComponent
{
public ISite Site { get { return null; } set { Site = value; } }
public event EventHandler Disposed;
public void Dispose() {}
};
System.ComponentModel.Component ob = new Component();
System.ComponentModel.IComponent inter;
// OK, Component implements IComponent
inter = (System.ComponentModel.IComponent)ob;
// OK, IComponent contains a Component
ob = (System.ComponentModel.Component)inter;
// OK, NotAComponent implements IComponent
inter = new Urg();
// Exception, NotAComponent does not contain a Component
ob = (System.ComponentModel.Component)inter;
Greetings,
Wessel