C# - Simple Class Library and how to use it in another project
C# - Simple Class Library and how to use it
- Create a Class Library Project
- Create a new project - Console Application
- 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.
- Add a reference to your Class Library project
- 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();
}
}
}
- Add preprocessor directive
- 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();
}
}
}
- Build the project and make sure the build succeeded:
- Run the project
- Success !
So, you’ve learned how to create and use a simple Class Library.
Comments
Post a Comment