C# – How to build own libraries in C#

Ever building a code, which you would like to have in every program you make? Most probably. So, if you are coding in C#, this is the way to do it – through libraries.

cs

So, these are the steps:

  1. In Visual Studio – File>New Project>Class Library.
  2. Then open the class library and make the classes. Compile it. You need to make sure that a *.dll file is generated in the debug folder of the project.
  3. When you want to use the library in a new project, go to References in the Solution Explorer and add the library *.dll file through the browse button. Then everything works perfectly, even the intelli-sense.
  4. Here are some screenshots 🙂

save1 save2

And here comes the code, executed with a library.

The code:

class ExtTest
{
    static void Main()
    {
        cls_worker w1 = new cls_worker("Vityata", 1000);
        cls_worker w2 = new cls_worker("Vitosh", 500);

        w1.Greet();
        w2.Greet("evening");
    }
}

The library:

using System;

public class cls_worker
{
    string _name;
    double _salary;

    public cls_worker(string name, double salary)
    {
        _name = name;
        _salary = salary;
    }

    public void Greet(string time_of_day = "morning")
    {
        string str_greeting_m = "Good morning, my name is {0}. My salary is {1}.";
        string str_greeting_e = "Good evening, my name is {0}. My salary is {1}.";

        if (time_of_day == "morning")
        {
            Console.WriteLine(str_greeting_m, Name, Salary);
        }
        else
        {
            Console.WriteLine(str_greeting_e, Name, Salary);
        }
    }

    public string Name
    {
        get
        {
            return _name;
        }
    }
    public double Salary
    {
        get
        {
            return _salary;
        }
    }

}

Enjoy it! 😀