12

High-level question here:

I have spent a lot of time today educating myself on basic high-level concepts such as APIs, static and dynamic libraries, DLLs and marshaling in C#. Gaining all of this knowledge led me to what seems like a pretty basic question, and probably demonstrates a hole in my understanding of these concepts:

What I know:

  • DLLs may contain classes which in turn contains various class-members such as methods and fields, several of which I might want to utilize in my program
  • In C# we use the keyword "using" at the top of the code, to define a namespace we want to include in our program

What I do not get:

I was under the impression that the actual methods were defined in the DLLs. How does my program find the actual functions that are defined in the DLLs, when all i give them is a namespace? It seems more intuitive to me to have "using XYZ.dll" at top, rather than "using XYZ_namespace".

Thanks a lot for helping me fill in the gaps here.

EDIT: Modified post to be specific to C#.

EDIT 2: For other people that wonder how their C# application actually gets a hold of the types made available through "using namespaceX", this is a good resource (in addition to the helpful posts below): http://broadcast.oreilly.com/2010/07/understanding-c-namespaces-and.html.

Basically the type you would like to use resides in libraries and you have to set Visual Studio to reference these libraries in order to make it possible to "use" its namespace in your code.

Magnus
  • 6,791
  • 8
  • 53
  • 84
  • C#: A dll or exe is an Assembly. An assembly may have internal classes and functions for use within it (internal limits access to the same assembly), within any number of namespaces. Namespaces are what contain classes, and are how you address them. Assembly references are on a per project basis, while namespace references are per file. – Magus Nov 18 '13 at 20:04
  • This question is all over the place. Namespaces vs DLL's. Include vs using. How methods are defined in C++ vs C#. Assemblies vs DLL's. The many differences between C++ (managed and unmanaged) and C# in general. There seems to be a plethora of answers and detail clarifications. Voting to close. I recommend you focus on one language at a time. – P.Brian.Mackey Nov 18 '13 at 20:08
  • @P.Brian.Mackey I will modify my post to be C# specific, if it is unclear. The question remains the same for a few other languages though. Using C# syntax: How does "using" a namespace make sense, when the methods I want to get hold of in fact reside inside a DLL. – Magnus Nov 18 '13 at 20:17

5 Answers5

7

DLLs contain many routines / methods we might want to use in our programs

Partially correct. .Net DLLs contain Classes, and these classes contain Members (Fields, Constants, Methods, Properties, Events, Operators, Indexers).

.Net is strictly OOP, and it does not allow code "floating in limbo". Everything is defined inside classes.

Classes are organized in Namespaces just to keep a naming separation and organization. Think of namespaces as "folders" that contain one or more classes, and that might be defined in one or more assemblies (DLLs).

For example, Classes inside the System namespace are defined in 2 assemblies (DLLs): mscorlib.dll and System.dll.

At the same time, these 2 assemblies contain many different namespaces, so you can think the Assembly to Namespace relation as a Many-to-Many.

When you put a using directive at the beginning of a C# code file, you're telling the compiler "I want to use classes defined in this Namespace, no matter what assembly they come from". You will be able to use all classes defined in such namespace, inside all assemblies Referenced from within the current project.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Thank you, that is very helpful. I sort of struggle a bit understanding that the System namespace is defined in 2 assemblies (DLLs). I thought the DLL files are the outer-most bucket. Then inside is several namespaces which in turn contains several classes. Is that not right? Is the System namespace replicated across several DLL files? Thanks again. – Magnus Nov 18 '13 at 20:31
  • @user3006096 yes and no. [Tim S.'s comment](http://stackoverflow.com/questions/20056681/relation-between-dlls-and-namespaces-in-c-sharp-c/20056937?noredirect=1#comment29874750_20056837) explains that perfectly. – Federico Berasategui Nov 18 '13 at 20:34
  • Thanks a lot HighCore. Among other things, your posted helped me understand the many-to-many relation that initially was confusing to me. – Magnus Nov 18 '13 at 21:10
  • Sorry if I am using the comment section wrong here, but just one last question: How come the System namespace is specified in several assemblies / DLLs? Isn't it enough to have that full namespace in one file? Or do they want to have different methods in different DLL files, but at the same time in same namespace, because they sort of belong together? Thanks again – Magnus Nov 18 '13 at 21:14
  • @user3006096 why do you keep talking about Methods? again, methods belong into a Class, they cannot live outside a class definition. And yes, they put different `classes` in the same namespace, even though they might be defined in different assemblies, because they belong together. – Federico Berasategui Nov 18 '13 at 21:16
  • Just mentioned methods, because that was the part of the DLL that I wanted. I realize that a DLL contains namespaces, which contains classes (and/or other types), which contains class-members. You are correct, it should instead more generally say types (or classes as you wrote). Thanks for clarifying. – Magnus Nov 18 '13 at 21:29
5

In C#, DLLs (aka assemblies) contain classes (and other types). These types typically have long full names, like System.Collections.Generic.List<T>. These types can contain methods.

In your References area, you have references to assemblies (this is part of your .csproj file). In a .cs file, you don't need to include any using to reference this DLL, because it's already referenced in your .csproj file.

If you include a line like using System.Collections.Generic;, that tells the C# compiler to look for System.Collections.Generic.List<T> when you type List<T>. You don't need to do it that way, however: you can simply type System.Collections.Generic.List<T>.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • 1
    Don't DLLs contain namespaces? Even if you don't declare one, that simply adds the classes to the global namespace, iirc. Classes and Structs are a level below in the hierarchy. – Magus Nov 18 '13 at 20:13
  • @AlexBeisley Yes and no. A "namespace" isn't a real structure in any sense, it's just a logical grouping. The assembly doesn't declare anywhere that it contains the `System.Collections.Generic` namespace, it just contains the type `System.Collections.Generic.List`. – Tim S. Nov 18 '13 at 20:16
  • @TimS. So, in References i specify the DLLs my code should utilize, and then i can use methods from namespaces within those DLLs? Is there anyway to see this Reference area in Visual Studio (just in case i want to add other DLLs, that is not added by default)? – Magnus Nov 18 '13 at 20:36
  • 2
    Yes, in the Solution Explorer, under your project, it's a folder-like item. See http://iprodeveloper.com/content/content/65330/references_f01.jpg for a pic – Tim S. Nov 18 '13 at 20:41
  • @TimS. Ah, fantastic, I see on the reference list CSharp.dll, System.dll, and a few others. It does not include mscorlib.dll, but i guess that is okay as all the types I need are covered in namespaces in the currently referenced DLLs. – Magnus Nov 18 '13 at 20:50
  • @TimS. Just one last clarifying point: Can the System namespace be defined in several DLLs, as HighCore indicated? – Magnus Nov 18 '13 at 21:03
  • @user3006096 yep, that's accurate. – Tim S. Nov 18 '13 at 21:03
  • @TimS. Finally, as a side-note, just to be clear in your post perhaps include the concept of namespaces. So, at that start, instead say: "In C#, DLLs (aka assemblies) contain classes (and other types), defined within namespaces. ...". Then: "Let's say you want to use List in your code. This collection resides in the System.Collections.Generic namespace. The only benefit of writing "using System.Collections.Generic" at the top of your code, is that you do not have to include the entire path every time you write List. – Magnus Nov 18 '13 at 21:04
  • It can either be to not include the full name of a class in your code, or it can be to differentiate between different classes with the same name (residing in different namespaces). Although you should, as a general rule, avoid having classes with the same name, it happens once in a while. Using the correct `using` statement will choose the right one for you that will actually be used. – Arve Systad Nov 18 '13 at 21:41
  • Guys, made an edit to my main post in order to be more helpful to other people that find this thread. It should be fine, but in case something is inaccurate, just let me know. Thanks. – Magnus Nov 18 '13 at 23:22
3

I was under the impression that the actual methods were defined in the DLLs. How does my program find the actual functions that are defined in the DLLs, when all i give them is a namespace?

The process of finding the correct code occurs through static or dynamic binding and also assembly binding. When you compile the code static binding will tell you if you wrote bad code or forgot to add a reference:

ClassInADifferentAssembly.M(); //Generally this will static bind and cause a compiler error if you forgot to include a reference to DifferentAssembly

Unless you are dealing with dynamic or reflection then you have static binding. Assembly binding is a different process. The overall process is complex, but basically assemblies are discovered in the the GAC, current location or you can even handle an event yourself, AppDomain.AssemblyLoad.

So when you add a using statement then static binding can successfully find the correct code in the context. However, you can still receive a runtime error if later the assembly fails to bind at runtime.

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • That is really useful info. So step by step (using List as an example): 1) I compile my program (which contains "List list = ..." as well as "using System.Collections.Generic" at top). 2) The compiler looks through all the DLLs in my reference list and static binds them to my program. 3) The compiler searches through all the bound DLLs for the System.Collections.Generic namespace. 4) Once it has found it, all is good. Is that about right? – Magnus Nov 18 '13 at 21:45
  • I caution focusing on the how's of static binding. How a process is performed by the compiler quickly leads to implementation details. In future versions of C# or another implementation of the CLR may not follow the same implementation . Its important to recognize static binding and assembly binding as two distinct processes and simply not to confuse the two. – P.Brian.Mackey Nov 18 '13 at 21:52
1

DLL is short for dynamic link library. And can be a class library containing classes, methods etc that can all be put under different namespaces.

So first you have to add a reference to the DLL into your project. When that is done, you then use a keyword such as "using" to basically shorten the path to reach the methods/classes in that particular namespace.

Example namespaces

Namespace.Something.SomethingMore.Finally.Just.One.More
Namespace.Something.SomethingMore.Finally.Just.One.More2

To reach classes under those namespaces you can do either of the following

using Namespace.Something.SomethingMore.Finally.Just.One.More;
using Namespace.Something.SomethingMore.Finally.Just.One.More2;

// Now you can access classes under those namespaces without typing the whole namespace
// Like in the row below
Class.GetData();

If you did not have the usings, you would still be able to access those classes. But would then have to type

Namespace.Something.SomethingMore.Finally.Just.One.More.Class.GetData();
Namespace.Something.SomethingMore.Finally.Just.One.More2.AnotherClass.GetData();
Armen Abrami
  • 224
  • 2
  • 8
0

DLLs have a collection of functions. You can calls these functions by one of 2 ways: link with the DLLs export library (a lib file) or do the link in runtime: Call LoadLibrary() Call GetProcAddress and provide the name of the function you want. You'll need to cast it to the actual type (function pointer). Call the function via the new function pointer. Pretty simple stuff, just read it on MSDN. C++ namespaces are just a part of the function name. You can view what functions are exported from a DLL by using a tool called Dependency Walker.

egur
  • 7,830
  • 2
  • 27
  • 47