Class vs. DLL

P

Paolo

Learning C# using Console applications. I want to develop some utility
functions (wrapping up some of the Console class methods) so that I can have
them available for any program that I write.

If I create a DLL do I include these functions by adding it as a reference
in my projects?

If I create a Class do I add the 'using' directive to include them?

Are there any pros/cons with respect to either approach?

Thanks
 
G

Göran Andersson

Paolo said:
Learning C# using Console applications. I want to develop some utility
functions (wrapping up some of the Console class methods) so that I can have
them available for any program that I write.

If I create a DLL do I include these functions by adding it as a reference
in my projects?

If I create a Class do I add the 'using' directive to include them?

Are there any pros/cons with respect to either approach?

Thanks

You can create a new project in the solution and create one or more
classes in it. This will then be compiled into a separate dll. To use
the classes from the application you need to add a reference to the project.

To use the classes in another solution, you can either add the project
to the solution and add a reference to the project, or add a reference
to the compiled dll file.

(Of course you can also develop the project separately in it's own
solution.)

If a class is in a different namespace (which it usually would be if
it's in a separate project), you either can use a using directive to
import the namespace or specify the full class name (i.e.
namespace.classname) everytime you use the class.
 
P

Peter Rilling

As you are trying to do, it is always a good practice to move common
functionality away from the application so that it can be shared.

Now some clarification regarding your "class" vs. "dll" question. A DLL is
a container for classes, therefore when you create a class, it is compiled
into a DLL/Assembly. You can simply create another "library" project and
move your classes/code from the console project. Once done, then yes, you
have to add a reference to that assembly in your console application.

As to whether you use a "using" statement depends on your namespaces. If
your choose to give your library code a different namespace from your
application (which you will probably want to do since this code will be
shared) then you will also need to add a "using"directive in any application
file that references your utility classes.
 
A

Arne Vajhøj

Paolo said:
Learning C# using Console applications. I want to develop some utility
functions (wrapping up some of the Console class methods) so that I can have
them available for any program that I write.

If I create a DLL do I include these functions by adding it as a reference
in my projects?

Not really.

The stuff in the DLL is made available not included.
If I create a Class do I add the 'using' directive to include them?

Not really.

The using allows you to refer to class names without their namespace.
Are there any pros/cons with respect to either approach?

You put compile one or more classes to a DLL. Good.

Arne
 
D

Duggi

Learning C# using Console applications. I want  to develop some utility
functions (wrapping up some of the Console class methods) so that I can have
them available for any program that I write.

If I create a DLL do I include these functions by adding it as a reference
in my projects?

If I create a Class do I add the 'using' directive to include them?

Are there any pros/cons with respect to either approach?

Thanks

As I see in your case, go for a DLL version of implementation. I see
that as a best practice.

-Cnu
 
P

Paolo

Peter: thanks for the clear explanation, which clarifies my supplementary
questions regarding namespaces and adding references.

As I understand it, if I choose a 'Class Library' as my project this gets
compiles into a DLL.
 
P

Paolo

Goran: thanks for the clarification.

Göran Andersson said:
You can create a new project in the solution and create one or more
classes in it. This will then be compiled into a separate dll. To use
the classes from the application you need to add a reference to the project.

To use the classes in another solution, you can either add the project
to the solution and add a reference to the project, or add a reference
to the compiled dll file.

(Of course you can also develop the project separately in it's own
solution.)

If a class is in a different namespace (which it usually would be if
it's in a separate project), you either can use a using directive to
import the namespace or specify the full class name (i.e.
namespace.classname) everytime you use the class.
 
P

Paolo

Cnu: VSC# Express allows a 'Class Library' project to be created - there is
no separate 'Create DLL' project type, so I presume a Class Library will be
compiled to a DLL.

Thanks
 
P

Paolo

Peter: a further question. Do I have to make the member methods of my
'Utilities' class/DLL public so that applications which use them can 'see'
them?
 
P

Peter Rilling

Yes: A class library project compiles to a DLL.

Yes: The scope must be public in order for other classes to access them
(unless you are creating something that relies on inheritance which would
then use protected scope).
 
A

Arne Vajhøj

Paolo said:
How does the application 'know' about my functions?

Adding a reference does just that add a reference - it does not
include the functions.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top