Wednesday, July 9, 2008

C# Basics


Few Command line options
/out Name of the assembly to be created.
/target:exe This option builds a single-file *.exe assembly.
/target:library This option builds a single-file *.dll assembly.
/target:module This option builds a module.Modules are elements of multiple assemblies
/target:winexe Same as target:Exe, but prevents a console window from appearing in the background.

Ex 1:
using System;

class MyClass
{
static void Main()
{
Console.WriteLine("hello");
}
}

csc /out:1.exe /target:exe 1.cs

Even though System.Console is not included, mscorlib.dll loads this automatically.

Ex 2:
using System;
using System.Windows.Forms;

class MyClass
{
static void Main()
{
Console.WriteLine("hello");
MessageBox.Show("Hello Manni");
}
}

csc /out:1.exe /target:exe /reference:system.Windows.Forms.dll 2.cs

Multiple dll's can be reference by
csc /out:1.exe /target:exe /reference:system.Windows.Forms.dll;System.Drawing.dll 2.cs


Ex 3: Source file can be split into multiple files

//file 3.cs
using System;
using System.Windows.Forms;

class Myclass
{
public void Show()
{
MessageBox.Show("Hello Ramesh");
}

}

//file 2.cs
using System;

class MyClass2
{
static void Main()
{
Myclass c = new Myclass();
c.Show();
}
}


csc /out:1.exe /target:exe /reference:system.Windows.Forms.dll 2.cs 3.cs
or
csc /out:1.exe /target:exe /reference:system.Windows.Forms.dll *.cs
*.cs includes all cs files in the directory

Response Files (.rsp): You can also create a response file and include all your csc compile options

file: test.rsp
/out:1.exe
/target:exe
/reference:system.Windows.Forms.dll
2.cs 3.cs

csc @test.rsp

C# By default has a response file called csc.rsp located in the same directory as csc compiler (C:\Windows\Microsoft.NET\Framework\v3.5).
If you open this file, you will find various assemblies listed using /references.
This file is auto included when you compile any file using cse.exe. If you want to disable the inclusion of csc.rsp, then
csc /out:test.exe 1.cs /noconfig

No comments:

Post a Comment