C# - Simple Class Library and how to use it in another project

C# - Simple Class Library and how to use it

  1. Create  a Class Library Project

  1. Create a new project - Console Application


  1. Right click on the newly created project and Set it as a StartUp Project

Reason: You cannot run a Class Library on it’s own, it needs to be called from another project.


  1. Add a reference to your Class Library project

  1. In the SimpleClassLibrary Project - rename the Class to PrintOK and add a public method WriteOK()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace SimpleClassLibrary
{
public class PrintOK
{
    public void WriteOK(){
     Console.WriteLine("OK");
     Console.ReadLine();
}
}
}

  1. Add preprocessor  directive

  1. In the UsingClassLibrary project, add the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleClassLibrary;
namespace UsingClassLibrary
{
class Program
{
   static void Main(string[] args)
    {
        PrintOK p = new PrintOK();
        p.WriteOK();
    }
}
}
  1. Build the project and make sure the build succeeded:

  1. Run the project
  1. Success !

So, you’ve learned how to create and use a simple Class Library.

Comments

Popular posts from this blog

AWS CLI - INSTALLATION AND CONFIGURATION

Upload a file to a Web server in ASP.NET by using Visual C# .NET