Components in C#

Introduction

The components creation and usage in C# is much more simple than earlier technologies like C++, ATL COM. As we know Components represent all kinds of elements that pertain to the piecing together of software applications. Among other things, they may be simple files, or libraries loaded dynamically. 

Component is a class with cleanup and containment. A component can be hosted in a container, and has the ability to query and get services from its container. Containment is logical and does not have to be visual. These components can be deployed in middle tier container as business components. Example database components deployed in middle tier.

We will see how to create a simple component in c# and how to use them in a client program. For an example I am taking Arithmetic component, which does the addition. This component has two properties and a method. Properties take input for addition and method Sum(...) does the addition. 

We will create a component called csAddComp1 and package this component into a dll (Dynamic Linked Library). And we will use this library in a client program for accessing the component.

To create properties in C# we have get & set accessors. The get accessor is used for getting (reading) and set accessor for setting (writing) the property.

In my example we have two properties varI and varJ. Since these properties are read-write properties to implement a read & write we need to use both get & set accessors for each property. I am using namespace in my example for hiding the classes we are creating inside. For easy understanding I made more comments in example code.

Component Program

using System;
namespace CompCS
{
public class csAddComp1
{
private int i=0,j=0;
public int varI
{
get { return i; } //this is property get for varI
set { i=value; } //this is property set for varI
} 
public int varJ
{
get { return j; } // this is property get for varJ
set { j=value; } // this is property set for varJ
}
public int Sum()
{
return i+j; //this returns the sum of two variables
}
} //end of class
} //end of namespace

To package the component as dll there is slight change in usual compilation process. Its little complicated process when compared to normal stand-alone program compilation.

csc /out:csAddComp1.dll /target:library csAddComp1.cs

Here the /out switch to put the compiled component in the relative subdirectory & file for convenience. Likewise, we need the /target:library switch to actually create a DLL rather than an executable with a .dll file extension.

In client program we will use a simple keyword called using <namespace>. Which will refer to the component.

Client Program

using System;
using CompCS;
class clAddComp1
{
public static void Main()
{
csAddComp1 addComp= new csAddComp1();
addComp.varI=10; //property set for varI
addComp.varJ=20; //property set for varJ 
//below property get for varI
Console.WriteLine("variable I value : {0}",addComp.varI);
//below property get for varJ
Console.WriteLine("variable J value : {0}",addComp.varJ);
// calling Sum(..) method
Console.WriteLine("The Sum : {0}",addComp.Sum());
} //end of Main
} // end of Class

Program Output

C:\csharp\progs>clAddComp1
variable I value : 10
variable J value : 20
The Sum : 30

Since our component is residing in csAddComp1.dll so our client program has to refer it at the time of compilation for that we have /reference compilation switch. 

csc /reference:csAddComp1.dll /out:clAddComp1.exe clAddComp1.cs


Similar Articles