Tuesday, November 29, 2011

How to compile and run c# codes in Linux

Here is how to write,compile and run C# programs in Linux OS.

First you need to get and install mono for linux.For this open the terminal and type

" sudo apt-get install mono-xsp  mono-mcs "


After installing mono you are ready to run or compile any cs file.
For simplicity here is how to write a simple c# hello world program.

open gedit and create a file called helloworld.cs.You can simply do this via terminal.

"gedit helloworld.cs"


After that write the program below to the file and save it.It is a simple c# code which prints "hello world" in the command line


-------------------------------------------------------------------------
Using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }
}

------------------------------------------------------------------------

Now compile the code using mono c sharp compiler and make the executable output using the command below.

"mcs -out:hw.exe helloworld.cs"


Now you will be having the executable output file called hw.exe.So as the final step use mono command to run the exe.

"mono hw.exe"






And thats it.This is a simplest kind of program you can write and mono is capable of making and run very complex cs code even.Also you can use mono to run asp.net web pages in Linux  just like in iis :)

1 comment:

  1. hi thanks for the tips im following now
    i have some cool tools for blogs
    http://add-new-people.blogspot.com/

    ReplyDelete