Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
ASP.Net 4 Hosting is here
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Visual Studio .NET » Tutorial: Creating C# Class Library (DLL) Using Visual Studio .NET

Tutorial: Creating C# Class Library (DLL) Using Visual Studio .NET


This step-by-step tutorial shows you how to create a class library (DLL) in Visual Studio .NET.

Author Rank:
Total page views :  83650
Total downloads :  327
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
mcMathProject.zip
 
Become a Sponsor

Creating a DLL using Visual C# is piece of cake. Believe me its much easier than VC++. I have divided this tutorial in two parts. 1. Building a Class Library, and 2. Building a client application to test the DLL.

Part 1: Creating a Class Library (DLL)

Create an Empty Class Library Project

Select File->New->Project->Visual C# Projects->Class Library. Select your project name and appropriate directory using Browse button and click OK. See Figure 1.




 

Figure 1.

Project and Its files

The Solution Explorer adds two C# classes to your project. First is AssemblyInfo.cs and second is Class1.cs.  We don't care about AssemblyInfo. We will be concentrating on Class1.cs. See Figure 2.

Figure 2.

The mcMath Namespace

When you double click on Class1.cs, you see a namespace mcMath. We will be referencing this namespace in our clients to access this class library.

using System;
namespace
mcMath
{
/// <summary>

///
Summary description for Class1.
/// </summary>

public class
Class1
{
public
Class1()
{
//
// TODO: Add constructor logic here
//
}
}
}

Now build this project to make sure every thing is going OK. After building this project, you will see mcMath.dll in your project's bin/debug directory.

Adding Methods

Open ClassView from your View menu. Right now it displays only Class1 with no methods and properties. Lets add one method and one property. See Figure 3.

Figure 3.

Right click on Class1->Add->Add Method... See Figure 4.

Figure 4.

C# Method Wizard pops up. Add your method name, access type, return type, parameters, and even comments.  Use Add and Remove buttons to add and remove parameters from the parameter list respectively. I add one test method called mcTestMethod with no parameters. See Figure 5.

Figure 5.

I am adding one more method long Add( long val1, long val2 ). This method adds two numbers and returns the sum. Click Finish button when you're done. See Figure 6.

Figure 6.

The above action adds two method to the class and methods look like following listing:

/// <summary>
//
//This is a test method
/// </summary>

public void
mcTestMethod()
{
}
ublic long Add(long val1, long
val2)
{
}

Adding Properties

Open C# Property Wizard in same manner as you did in the case of method and add a property to your class. See Figure 7.

Figure 7.

This action launches C# Property Wizard. Here you can type your property name, type and access. You also have options to choose from get only, set only or get and set both. You can even select if a property is static or virtual. I add a property Extra with public access and bool type and get/set option set. See Figure 8.

Figure 8.

After adding a method and a property, our class looks like Figure 9 in Class View after expanding the class node.

Figure 9.

If you look your Class1 class carefully, Wizards have added two functions to your class.

/// <summary>
//
//This is a test property
/// </summary>

public bool
Extra
{
get

{
return true
;
}
et

{
}
}

Adding Code to the Class

Add this code ( bold ) to the methods and property now. And now I want to change my Class1 to mcMathComp because Class1 is quite confusing and it will create problem when you will use this class in a client application. Make sure you change class name and its constructor both.

Note: I'm not adding any code to mcTestMethod, You can add any thing if you want.

using System;
amespace
mcMath
{
/// <summary>

///
Summary description for Class1.
/// </summary>

///

public class
mcMathComp
{
private bool bTest = false
;
public
mcMathComp() 

//
// TODO: Add constructor logic here
//
}
/// <summary>

///
//This is a test method
/// </summary>

public void
mcTestMethod()
{
}
public long Add(long val1, long
val2)
{
return
val1 + val2;
}
/// <summary>

///
//This is a test property
/ </summary>


public bool
Extra
{
get
 
eturn
bTest;
}
set

{
bTest = Extra ; 
}
}
}
}  

Build the DLL

Now build the DLL and see bin\debug directory of your project. You will see your DLL. Piece of cake? Huh? :).  

Part 2: Building a Client Application

Calling methods and properties of a DLL from a C# client is also an easy task. Just follow these few simple steps and see how easy is to create and use a DLL in C#.

Create a Console Application

Select File->New->Project->Visual C# Projects->Console Application. I will test my DLL from this console application. See Figure 10.

 

Figure 10.

Add Reference of the Namespace

Now next step is to add reference to the library. You can use Add Reference menu option to add a reference. Go to Project->Add reference. See Figure 11.

 

Figure 11.

Now on this page, click Browse button to browse your library. See Figure 12.

Figure 12.

Browse for your DLL, which we created in part 1 of this tutorial and click Ok. See Figure 13.

 

Figure 13.

Add Reference Wizard will add reference of your library to the current project. See Figure 14.

 

 Figure 14.

After adding reference to mcMath library, you can see it as an available namespace references. See Figure 15.

 

Figure 15.

Call mcMath Namespace, Create Object of mcMathComp and call its methods and properties.

You are only one step away to call methods and properties of your component. You follow these steps:

1. Use namespace

Add using mcMath in the beginning for your project.

using mcMath;

2. Create an Object of mcMathComp

mcMathComp cls = new mcMathComp();

3. Call Methods and Properties

Now you can call the mcMathComp class method and properties as you can see I call Add method and return result in lRes and print out result on the console.

mcMathComp cls =
new mcMathComp();
long
lRes = cls.Add( 23, 40 );
cls.Extra =
false
;
Console.WriteLine(lRes.ToString());

Now you can print out the result.

The entire project is listed in the following Listing:

using System;
using
mcMath;
namespace
mcClient 
{
// <summary>

/// Summary description for Class1. /// </summary>

class
Class1 
// <summary>

///
The main entry point for the application.
// </summary>

[STAThread]
static void Main(string
[] args) 
{
mcMathComp cls =
new
mcMathComp();
long
lRes = cls.Add( 23, 40 );
cls.Extra =
false
;
Console.WriteLine(lRes.ToString());
}
}


Now build and run the project. The output looks like Figure 16.

 

Figure 16.


Login to add your contents and source code to this article
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
 Comments
Creating C# Class Library (DLL) Using Visual Studio .NET by Marselle On March 28, 2007
I am trying to add property's Methods, and Fields in Visual Studio 2005 .Net and when I right click on the Class1 in my Library that I create.. the add --> Methods and so on.. is not there.... I can do this in Visual Studio 2003.. but not in 2005 can yo assist in any way.. any ideas
Reply | Email | Delete | Modify | 
Add method,.. in VC# 2005 by Mohammad On August 23, 2008

Hi Marselle,

=> I'm using this way :

Goto 'Class View' pane. Just below the title of pane, there is a button 'View Class Diagram', click that (or RClick on the 'Class View' pane and choose 'View Class Diagram'), in this view you can add methods,... in various ways.

Reply | Email | Delete | Modify | 
dll by sachin On August 31, 2008
hi sir i am sachin gniit student in new delhi have completed bca . iam perfect at c# language but when question comes about c#.net i get confused that should i prepare c# console language or windows application . if i go to in company i will do work on console or windows application this question is in my mind due to this question i am unable to concerate on console or windows application plz plz plz suggest me.....
Reply | Email | Delete | Modify | 
Re: dll by Manish On April 8, 2009
Hi Sachin,
 
Not confused with this. It depends on the company or the projects. As u said u are good in C# then u can easily handle both type of projects.Only deference between these projects is UI. 
Reply | Email | Delete | Modify | 
thanks! by Dan On September 28, 2009
9 years later and this article is a still a godsend. I spent hours searching before finding this working example.  Thank you.
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.