First you need to understand what a class library is. You are here, it signifies your interest on class library. This small article describes what a class library is and how to develop and use a class library in Dot Net Framework using C#.
Well, a class library can be considered as prewritten component and is a collection of subroutines which can be used by other programs or projects. The flavour of creating class library is that it supports Object Orientation(OO). According to the terminology, the moment you are creating a class library, you are creating a reusable component. Now, the following questions will come in your mind,
1. I can write straightforward code, what is the point of writing a library?
Response: Yes. Surely, you can. But if you modulerize, then maintainence will be easier. If tomorrow your client wants a change in the business logic, you can do the necessary changes on the particular function on library itself; All the running projects will be changed in one shot. Still have doubt? Put a comment, I will reply.
2. What are the functions/methods I suppose to put in the Class Library?
Response: Justified question. It depends upon the scenario. Different approaches are there. I am discussing one approach. First design the class diagram of the project (I am not discussing, the project management theory or UML here). Then develop the classes in a class library. Compile that library. Don't forget, in class library, you suppose to put your business logic only, not UI (User Interface) related code(s).
How to Develop and Consume a class library in Dot Net (with C#)?
Considering you are using at least a Visual C# Express Edition or any higher version of Visual Studio IDE.
Now it is your turn to think about the project. I am considering a simple class named Calc with Addition, Subtraction, Mutiplication and Division Functionality.
Step 1: Open Visual Studio (2010 I am using), and create a New Project from File menu. The following Dialog Box will appear (Fig 1).
Fig 1: New Project Dialog Box |
Step 2: Select Visual C# from the "Installed Template" (which is on the left pane) and take "Class Library" template from the Installed template list (See figure 1). Give a meaningful name of the project. Save on a folder which you can remember !!! Where to put names and Saving paths? See Fig 2.
Fig 2: Class Name and Save Location Options |
Step 3: A Class file will open in front of you (Refer the following image). Write down the methods. (If you need more classes, add class files from solution explorer - Fig 4.)
Fig 3: Default Class File with Solution Explorer |
Step 4: Save (from File -> Save) and build (From Build Menu) the project.
Step 5: You will get your dll file ready on Bin/Debug folder under your project directory.
Step 6: Now, it is the time to consume the dll. Take a new project. This can be a Console Application or Windows Application or a web application. You need to add the reference only to consume it. Exactly, 3 steps need to be performed as mentioned below.
Step 6.1. Open Solution Explorer --> Open References (For Console and Windows based apps only.) Right Click on References --> Click Add Reference. The Following Dialog Box (Fig 4) will appear.
Fig 4: Add Reference Dialog Box |
Step 6.2. The dll will be visible to your reference list. As depicted in Fig 5.
Fig 5: Reference Visible in References Folder |
Step 6.3. Instantiate Object of the class and Call the Methods.
It is done. You have understood what a class library is and how to consume that.
I know what you are feeling. You have some questions, if you are a seasoned developer. If you are just learning, learn the tips also. 2 questions are coming to my mind immediately. Let me share those with response. (If you have any other query, please put on the comment box)
1. When I use any Dot Net Framework Classes, a tooltip text appears behind Intellisense. Can I do that with my Class Library to help my Fellow Programmers?
Response: Well. This is the concept of Code Documentation. I always refer people to do this. This is a very good programming practice. First Write down the function and place your cursor above the function name. Like you put comment lines, put three forward slashes (/). Visual Studio automatically will put the documentation XML environment for you. Just Consider, I have the following function
public int Add2Integers(int FirstValue,int SecondValue)
{
return FirstValue+SecondValue;
}
Now, I want to add documentation for the above function. The moment I put three(3) for slashes(/) above the function, VS 2010 IDE will automatically type the following XML documentation for you
/// <summary>
/// Write Down the Summary Note you want to put /// </summary>
/// <param name="FirstValue">Write something here - About FirstValue Param</param>
/// <param name="SecondValue">Write something here - About SecondValue Param</param>
/// <returns>What kind of Return Value has been Defined</returns>
/// <param name="FirstValue">Write something here - About FirstValue Param</param>
/// <param name="SecondValue">Write something here - About SecondValue Param</param>
/// <returns>What kind of Return Value has been Defined</returns>
Fine. A tempting feature, isn't it? Yes. But the you need to perform some more steps for the consumer application. Now perform the following
1. Right Click on (Class Library) Project (From Solution Explorer)
2. Click on Properties.
(Both the steps can be performed in one shot, Double Click properties.
3. Click on Build (on the left pane) ---> Check on the Checkbox named "XML Documentation File".
4. Save the Project and ReBuild.
2. How to add a strong name assembly on my class library project?
Yes. Security is a crucial issue and should be handled. Not only strong naming, you may need to handle delay signing also. Perform the following steps.
1. Open Properties
2. Click on the tab named "Signing*"
3. Check on "Sign in Assembly" and select Browse option (If you have any) and select the file from the Browse Dialog box, or create a new one with snk extension.
Practical Perspective
Want to see an example where people can use this feature? A good article has already posted by my friend Vikas Ahlawat, which is there in the following link (After that blog post, I do not think posting one more article on the same topic):
That is it. Hope you enjoyed the article and learned something new. If this article helped you a bit, please put a comment and share with your friends. That will help me to contribute more.
Nobody is 100 % perfect. So, if I missed something, please mention that. Thanks in advance.
Sir,I'm not getting the tooltip due to some reason...just cant find why....otherwise itz awesome....
ReplyDeleteWhat tooltip you are talking about? Please clarify
ReplyDeleteSir khub helpfull blog....but sir 2 to question aache?
ReplyDelete1)Solution Explorer a right click korle,add service reference ta ki?
2)Ami 1ta strong name Calc.snk create korechi,but ei strong name ta use korbo kothay?
sir,strong name ta ki details a jodi bolen khub bhalo hoy....
Sir apni je bhave 3 layer bole chilen,sara jibon bhulbo na,sei water tank r pipe er example....
Thank u sir.....