Reusing C # Code Through Namespaces

I like to create a file full of custom functions that I have created that I can use in another project or something. Now I don't quite understand how to do this, usually in a language like php, you just create a php file and then include include ("cust_lib.php") or whatever file is called.

Now I think this process includes a library that has its own namespace, or either use custom_lib; or custom_lib :: inside a script (I don't want to discuss which path is best to go).

Is it correct? Or should I create a library and convert it to DLL, if so how should I do it, what is the syntax of the DLL inside, etc.

However, if its the only file in one project, I don't have to go that route? Can I just create a namespace and use that?

This is what I am currently working on and thought it would be something like this

namespace Custom_Lib{
  ~~functions to go here~~
}

      

However, functions must exist inside a class, right? So it will become something like

namespace Custom_Lib {
  class custom_lib {

    public string function1 (string input) {
      return input;
    }

  }
}

So some hints, pointers, examples would be appreciated, so I can wrap my head around this

Thank you Psy.

(Yes, I call them functions that only come from the long php / js background, etc.)

+2


source to share


3 answers


A common approach would be to create a class library project, put your classes and methods in that project, making sure the ones you want to open are public

. Then you add a reference to the resulting DLL file in your client projects and you will have the functionality from the class library available to you.

Even if you decide to put it all in one file, I would recommend that you make it a class library, as I imagine it will make things easier. For example, consider the following scenarios:

  • You have chosen to put it in a file and include a copy of this file in all projects where you want to use it. Later you will find an error in the code. You will now have multiple copies of the file with the bug fixed.
  • You have chosen to put it in a file and include the same file in all projects. Now, if you want to change some behavior in it, you change the behavior for all projects using it.

In these two cases, keeping it as a separate project will make your job easier:

  • You will only have one copy of the support code
  • You can decide whether to update the DLL used by a specific project when you create updates to the class library.


As for syntax problems: yes, all methods must exist inside the class. However, if a class is just a container of methods, you can make it (and methods static

):

public static class CustomLib
{
    public static string GetSomethingInteresting(int input)
    {
         // your code here...
    }
}

      

This way you won't need to create an instance CustomLib

, but it can just call the method:

string meaningOfLife = CustomLib.GetSomethingInteresting(42);

      

+6


source


In addition to Fredrik MΓΆrk's well-written and spot-on answer, I would add the following:

  • Avoid creating a single class that is a collection of kitchen functions / methods.


Instead, group related methods into smaller classes to make it easier for you and your library consumers to find the functionality they need. Also, if your library uses class level variables, you can limit their scope.

Also, if you later decide to add streaming capabilities to your library, or if your library is used in a multi-threaded application, static methods are likely to be a nightmare for you. This is a serious problem and should not be overlooked.

+1


source


There are no questions here. You answered yourself. Yes, you must build a class to include all helper methods. And yes, you can either compile it to a dll if you want to reuse it across multiple projects, or just add the source file to the project.

I usually declare the helper class and all of its functions static so that the class doesn't run every time I use it.

0


source







All Articles