How to Create a Class Library in C# .NET
A dynamic linking library (DLL) is linked to your program at run time. To demonstrate building and using a DLL, consider the following scenario.
To build the file MathLibrary.DLL, compile the two files Add.cs and Mult.cs using the following command line:
csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs
The /target:library compiler option tells the compiler to output a DLL instead of an EXE file. The /out compiler option followed by a file name is used to specify the DLL file name. Otherwise, the compiler uses the first file (Add.cs) as the name of the DLL.To build the executable file, TestCode.exe, use the following command line:
csc /out:TestCode.exe /reference:MathLibrary.DLL TestCode.cs
The /out compiler option tells the compiler to output an EXE file and specifies the name of the output file (TestCode.exe). This compiler option is optional. The /reference compiler option specifies the DLL file or files that this program uses.
sing 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;
}
}
}
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
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
![]() |
| Add caption |
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
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

No comments:
Post a Comment