What Is a C Component?


What Is a C# Component?

Because of the way C# works, any class fulfills the general definition of a component. For instance, once compiled, a class can be used in its binary form by any number of other applications. Does this mean that any class is a component? The answer is no. For a class to become a component, it must follow the component model defined by the .NET Framework. Fortunately, this is extremely easy to do: It simply implements the System.ComponentModel.IComponent interface. By implementing IComponent, a component satisfies the set of rules necessary to be a component compatible with the .NET Framework.

Although implementing IComponent is not difficult, in many situations a better alternative exists: A class can inherit System.ComponentModel.Component. The Component class provides a default implementation of IComponent. It also supplies other useful component-related features. Most components will inherit Component rather than implement IComponent themselves, because much of the clerical work has been done for you. Thus, in C#, creating a component requires no “heavy lifting” on your part.

Containers and Sites

Two other constructs are closely related to C# components: containers and sites. A container manages a group of components. Containers simplify programs that use multiple components. A site links containers and components. Both of these constructs are examined in detail later.

C# vs. COM Components

C# components are much easier to implement and use than are components based on COM. Those familiar with COM know that when using a COM component, you need to perform reference counting to ensure that a component stays in memory. In this scheme, each time a reference is added to a component, your code must make a call to AddRef( ). Each time a reference is removed, your code must call Release( ). The trouble is that this approach is error-prone. Fortunately, reference counting is not required by C# components. Since C# uses garbage collection, a component automatically stays in memory until there are no more references to it.

Because IComponent and Component are at the core of component programming, we will examine them next.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net