Compile DLL using Command Line C# Compiler


Introduction

Many users know how to create code library using Visual Studio IDE in GUI environment for making code reusable code so that multiple application can use that dll.but here I am writing something by which after reading this article you will be able to compile DLL using just command line C# compiler without opening Visual Studio IDE.

CompileDll1.gif

Language:

C# .net 2.0/3.5

Prerequisites:

Basic knowledge of DOS(cmd.exe) and C#.

Implementation

First let you tell what is out target is to compile a DLL (reusable code library) using command line c# compiler that is csc.exe.

Fire up Notepad or any other your favorite text editor Program.

As example we will build simple dll that will contain methods like add() , substract() multiply().

using System;
 
namespace SimpleMaths
{
   
public class Operations
    {
 
       
public static int add(int a, int b)
        {
           
return a + b;
        }
 
       
public static int substract(int a, int b)
        {
           
return a - b;
        }
 
       
public static int multiply(int a, int b)
        {
           
return a * b;
        }
    }
}


Save the file with <FileName>.cs

Now open Start>>VisualStudio2005/2008 >> Visual Studio tools>>Visual Studio Command Prompt.

Now in command Prompt Locate the Directory where you have saved the .cs file just created. Using CD command.

Here I have given myfile name "test.cs".

Now for compiling our dll using csc.exe we need to pass arguments you csc.exe like what type of file we want to generate, What will be name of output dll file and source file from which we are building our dll File.

csc /target:library /out:MyMaths.dll test.cs

CompileDll2.gif

Press Enter and you will get your desired DLL file generated!!

You just have created DLL fFile with Command Line C# Compiler. Now use that dll file in any .net project !!

Now you will raise question how do I compile multiple source file (.cs) files?

Well simple just execute commad like below one pass 2 names of source files in command while compiling it.

csc /target:library /out:MyMaths.dll test.cs test2.cs

Conclusion

This article teaches how to compile dll file in c# using command line C# Compiler of .net.