CasperT CasperT - 1 month ago 15
C# Question

When is it the right time to use C# class library (.dll)?

I'm a programmer who has never really used .dll files.
Of cause, when I need 3rd party software, such as a graphics library, a library to help me create graphs etc. I do add the references/ddl files to my program and use them in my code.

Also, it seems like you can use .dll for a lot of different things, so I'd like the topic to concentrate on C#.

Right now I am working on a sanitizing library(?) (I think that is the correct term), which will be full of relevant methods that can sanitize variables in all sorts of different ways.

What I want to know is:

would there be any advantage to:

1) Write the methods to class library -> compile/build it -> add the library as a reference to the program - which would need to sanitize some variables ?

Or would it be exactly the same if I where to:

2) Create a new SanitizeClass in the program -> add all the sanitize methods -> call the methods from the SanitizeClass in the different classes in the program that needs to sanitize variables

In general, I also want to know when it is an advantage to use compiled class libraries.
I'm thinking of speed, security, all of it.

Could anyone enlightenment me? :)

Answer

The key question is: does it make sense for more than one application to use the type? If so, it should be in a class library. If not, you may still want to put it in a class library just for the sake of separation (e.g. have one assembly per tier in an n-tier solution), but you could just put it in your application.

If your sanitization is general-purpose, then putting it in a class library would definitely be the right move.

I know of people who write almost no code in executable applications, and put almost everything in class libraries - so the application basically just wraps the class libraries. I don't tend to go quite that far myself...