Overview
The Component class provides the base implementation for the IComponent interface and enables object composition. It is the base class for all components that can be placed on a design surface.
Inheritance
System.Object
└─ System.MarshalByRefObject
└─ System.ComponentModel.Component
Namespace
System.ComponentModel
Assembly
System.dll
Members
| Member | Type | Description |
|---|---|---|
| Site | Property | Gets or sets the site of the component. |
| DesignMode | Property | Gets a value indicating whether the component is in design mode. |
| Dispose() | Method | Releases all resources used by the component. |
| Dispose(bool) | Method | Releases the unmanaged resources used by the component and optionally releases the managed resources. |
| Disposed | Event | Occurs when the component is disposed. |
Site Property
Gets or sets the ISite associated with the Component.
public virtual System.ComponentModel.ISite? Site { get; set; }
DesignMode Property
Indicates whether the component is currently being used in a design environment.
public bool DesignMode { get; }
Dispose Method
Releases all resources used by the component.
public void Dispose()
This method calls Dispose(true) and then suppresses finalization.
using System;
using System.ComponentModel;
public class MyComponent : Component
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
}
// free unmanaged resources
base.Dispose(disposing);
}
}
Dispose(Boolean) Method
Releases the unmanaged resources used by the component and optionally releases the managed resources.
protected virtual void Dispose(bool disposing)
Disposed Event
Occurs when the component is disposed.
public event EventHandler? Disposed;
Remarks
The Component class implements IComponent and provides a basic implementation for component lifecycle management. It is commonly used as a base class for controls, services, and other reusable objects.
Examples
Creating a custom component that overrides Dispose:
using System;
using System.ComponentModel;
public class CustomTimer : Component
{
private System.Timers.Timer _timer;
public CustomTimer()
{
_timer = new System.Timers.Timer(1000);
_timer.Start();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_timer?.Stop();
_timer?.Dispose();
}
base.Dispose(disposing);
}
}