Code reusability is very important in the software development process. We can save time and resources and reduce redundancy by using the code that have already been written.
A Class library is a good example of code reusability. In object-oriented programming , a class library is a collection of prewritten class es or coded templates which contains the related code. When we finish a class library, we can decide whether you want to distribute it as a third-party component or whether you want to include it as a DLL with one or more applications.
Suppose we have to write a modular application for an organization. Definitely, all the modules will use the similar kind of data or functionality. Suppose a developer who belongs Employee module write a class named ‘StringLibrary’ which contains the common methods for finding out that particular string starts with uppercase.
If these functionality also needed in Payroll Module then developers who belongs to payroll module can also use the same class.
In this article, I’ll create a simple utility string library that contains several string-handling methods using .net core in visual studio code. visual studio code is the open source, If you are new for Visual studio code read this article how to start with VS Code.
Create a Class Library
Open Visual Studio Code and create a class library project using the following ‘dotnet’ command:
F:\Documents\DotNetCoreExample-ClassLibrary\dotnet new classlib -o stringUtility
The above command will create a classLibrary project with the name ‘stringUtility’ (It will also create a ‘stringUtilty’ folder). If no name is specified, the name of the current directory will be used.
Now you can see the file list under the stringUtilty folder in the visual studio code explorer like below. Give the name class1.cs to stringLibrary.cs.
Write a function to find out whether given string starts with upper case or not:
using System; namespace stringUtility { public class StringLibrary { public bool StartsWithUpper(String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsUpper(ch); } } }
Write another function to check that given string is palindrome or not.
using System; namespace stringUtility { public class StringLibrary { public bool StartsWithUpper(String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsUpper(ch); } public bool IsStringPalindrome(string str){ string revs=""; for (int i = str.Length-1; i >=0; i--) //String Reverse { revs += str[i].ToString(); } if (revs == str) // Checking whether string is palindrome or not { return true; } else { return false; } } } }
Build Class Library
Now we need to build this class library. We use the ‘dotnet build’ command for build a project.
F:\Documents\DotNetCoreExample-ClassLibrary\stringUtilty>dotnet build
After running the above command build process will start for the ‘stringUtility’ project. After successfully build we can see bin/debug folder under the ‘stringUtility’ project.
After running the above command build process will start for the stringUtility project. After successfully build we ca see bin/debug folder under the stringUtility project.
Now let’s see how can be use this class library in another project.
Create a new Console Application
First we create a new folder named ‘ConsoleApplication’:
F:\Documents\DotNetCoreExample-ClassLibrary\md ConsoleApplication1
F:\Documents\DotNetCoreExample-ClassLibrary\cd ConsoleApplication1
And now create a new console application:
F:\Documents\DotNetCoreExample-ClassLibrary\ConsoleApplication1\dotnet new console
New console application will be create with all required files. Now we need to add reference of the ‘stringUtility’ class library into our console application.
F:\Documents\DotNetCoreExample-ClassLibrary\ConsoleApplication1>dotnet add reference ../stringUtility/stringUtility.csproj
After running above command, the reference `..\stringUtility\stringUtility.csproj` will be added to the project.

Next Steps
Now you can use the ‘StringLibrary’ class in the ‘consoleApplication1’ project. Open program.cs in consoleApplication1 project and use the ‘StartsWithUpper()’ method like this:
using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); stringUtility.StringLibrary clsString = new stringUtility.StringLibrary (); Console.WriteLine(clsString.StartsWithUpper("Sts")); } } }
Now you can run the ‘ConsoleApplication1’ using dotnet run command like this:
F:\Documents\DotNetCoreExample-ClassLibrary\ConsoleApplication1>dotnet run
leApplication
F:\Documents\Examples\DotNetCoreExample-ClassLibrary\ConsoHello World!
True