In C# you get relief from using #include and header files, although I don’t really have a problem with using the header files in C++, the complied assembly are self describing which means that code and metadata are generated during compilation. In the specification paragraph 1.2 discusses the Program Structure and shows you how to generate a dynamic linking library or dll at the command line and I will discuss that in this article. Of course I will also show how to do the compilation using Visual Studio. Like Eclipse, the Visual Studio system of development has a number of ways to do things, and you can add tools as you need to the Visual Studio Pro and Ultimate versions.
You will need to read the specification while we do this kind of side work of building the DLL and consuming them. What I find is that most students do not experience this in school but the use of DLLs is important in many software technologies that run on Windows or if you are running Mono on Linux, for instance Raspberry Pi with Mono on it, thanks to Steven Edouard, who just pointed that out to me. I will cover how to get Mono on your Raspberry PI, but if you
First, let’s examine the use of the Notepad, command line process. Open notepad and add the code you wish to use for the DLL, think of the DLL as a way to separate your classes from your project code. This makes your code easier to read, but you do need to document your DLLs as others will only be able to examine the DLL via the object browser.
Open the C# specification and go to paragraph 1.2, I discuss where you can get the C# specification in my blog and how to open the Visual Studio 2013 command line, although you can use earlier versions of Visual Studio command line for this blog:
- C#: The C# Language Specification and Hello World
- To find the command line compiler for Visual Studio: http://msdn.microsoft.com/en-us/library/ms229859(v=vs.110).aspx
Copy and paste the first example under 1.2 Program Structures, this will be your dynamic linking library into notepad.exe, save the file as indicated in the specification: acme.cs (not acme.dll, that will be the generated or compiled file). You will see a new file with the extension of DLL. Scroll down and you will be able to see the DLL consumed by an EXE.
Consuming the DLL
DLL are used everywhere in Windows. These are useful for stable classes that you use a great deal in your software. But they will not run by themselves, you need an EXE to use them. A little further on in the C# specification you will see that there is a shorter program that is set-up to consume the DLL. Now I am not going into to how the interface works, for now, let’s just accept that it works nicely. As the specification states, the acme.dll presents the Acme.Collections.Stack class, so let’s build the exe, first copy and paste the code into a new notepad file named test.cs (you name it when you save it of course), and save it into the same folder as the dll, make sure the names are different. Now compile the program as shown, the output is shown as well.
There are number of ways to build or compile a Visual Studio application, at the command line, MS Build, Visual Studio or Team Foundation Build. In my next blow we will use Visual Studio 2013 Ultimate to build the dll and exe.