Components in C#

Tuesday Nov 13th 2001 by Chandra Hundigam

How to create a component in C#.

Environment: C# .NET SDK Beta 2

The components creation and usage in C# is much more simple than earlier technologies like C++, ATL COM. As we know a 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.

A 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.

In this article, you will see how to create a simple component in C# and how to use it in a client program. For an example I will use an Arithmetic component, which does addition.

We will create a component called csAddComp1 and package it into a dll (Dynamic Linked Library). This component has two properties and a method. Properties take input for the addition and method called Sum().

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

In my example we have two properties, varI and varJ. These properties need to be written to and read from. To implement the reading and writing we need to use both get and set accessors for each property. I am using a namespace in my example int order to hide the classes being 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 and 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> <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 :

variable I value : 10
variable J value : 20
The Sum : 30

Since the component is residing in csAddComp1.dll, the client program has to refer it at the time of compilation. The /reference compilation switch will make this reference:

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

Further reading see the Microsoft .NET pages.


Download source - 32 Kb
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved