Ubuntu FAQ: Compiling and Running C# Programs

2013-01-08  Source: Original Site  Category:Ubuntu  Views:7 

Advertisement

Working on the rest, post suggestions as well. I am not skilled or experienced in C#

So you're willing to write your own C# programs, or simply compile someone else's code? This guide will get you started...

This FAQ assumes you're trying to compile error-free code. To actually learn the C# languages, see the How to start programming (http://ubuntuforums.org/showthread.php?t=333867) thread.

Also remember that although C# is now an ECMA standard, and mono and gmcs follow it (at this point, up to 2.0 fully), many popular libraries used by C# in Windows are not part of the standard.

0.a. Make sure the third party repositories are enabled

The easiest way to do this is open Applications->Add/Remove and select "All Available Applications".

0.b. Make sure the mono-gmcs package is installed

This package installs the essential tools needed to write and run C# programs.

sudo aptitude install mono-gmcs

1. Compiling your first C# program

Copy & paste this code into a new file. Save the file as Main.cs

//First C# Program
using System;

namespace Main
{
class Welcome
{
public static void Main( string[] args )
{
Console.Write("Hello world\n");
}
}
}//Matching brace. Some people can't be pleased...

Open a terminal, go to the directory where you saved Main.cs, and type:
gmcs Main.csIf all went fine, nothing is printed and you get back to the shell. Now, run it:
mono Main.exe"Hello World!" gets printed in the terminal.

Explanations: gmcs is the C# compiler
obviously, Main means that the compiler must compile this file.
mono Main.exe runs the newly compiled executable.

2. Paranoid programming is good for your health!

Many beginners want to avoid compiler errors and warnings at any cost, which often leads them to ignore the "cryptic" messages the compiler outputs and to tinker anxiously with the relevant line until the error goes away.
Don't fall in that trap! The compiler is your friend, the messages it outputs are kind advices rather than punishments. You should really take the time to try and understand those messages, this is a very good way to improve your code's quality.

As a matter of fact, most seasoned programmers set the compiler's warning level as high as possible, so that it catches the little errors they make when they aren't paying much attention.

**Help needed** Post in thread to recommend content or deletion of this section.

3. Useful tips

**Help needed** Post in thread to recommend content or deletion of this section.

4. Read the manual!

Plain old manpage:
man gmcs
man mono

Naturally, the Documentation and Tutorial on the Mono project site http://www.mono-project.com/ are good to visit.

You can use any editor, but MonoDevelop (http://www.monodevelop.com/Main_Page) seems to be the IDE of choice for many C# programmers.

Thanks to so many people @UF for their very useful help, this FAQ wouldn't be the same without them. In particular, this thread is based on aks44's thread on C and C++ (see link in sticky). Not only is this thread based on it, it is blatently copying it, I hope he doesn't mind...

Related articles
  • Ubuntu FAQ: Compiling and Running C# Programs 2013-01-08

    Working on the rest, post suggestions as well. I am not skilled or experienced in C# So you're willing to write your own C# programs, or simply compile someone else's code? This guide will get you started... This FAQ assumes you're trying to compile

  • Ubuntu FAQ: Compiling and Running Java Programs 2014-10-08

    Working on the rest, post suggestions as well. I am not skilled or experienced in Java So you're willing to write your own Java programs, or simply compile someone else's code? This guide will get you started... This FAQ assumes you're trying to comp

  • Ubuntu Beginner compiling and running c++ program 2012-12-09

    Hello I am new to linux. I want to compile and run a simple c++ hello word program, I use the following command g++ hello.cpp -o ./hello but it doesn't work. I've also tried to run the executable separately but it keeps saying bash: ./hello: Permissi

  • Ubuntu how to set gedit for compiling and running java programs 2012-03-20

    hi, i m newbie here....and i m using ubuntu 9.04. can anyone tell me tht how can i set gedit for compiling and running java programs....i have some threads but not anyone working for me....i very begginner here so plz tell me step by step info to set

  • Ubuntu set gedit for compiling and running java programs 2015-02-09

    hi, i m newbie...can anyone tell me how can i set gedit for compiling and running java programs.. thnx in advance

  • Ubuntu C++ Compiling and Running 2014-05-14

    I'm new to Ubuntu and also am just beginning to learn how to program. This afternoon I tried to set up a simple program to compile and run through the terminal, but it just gives me a > prompt that won't do anything after that. I downloaded/instal

  • compiling and running C programs 2014-05-12

    i am new to C programming and i need a way to compile and run my code.I've made a little research and i found that eclipse is a nice IDE.But i don't know how to configure it and generally... what do i need to download and install to make it work.I've

  • Compile and run java program by java 2012-11-27

    hai friends, How can i comiple and run java program using java code(with out using batch file) plz any one help me in t \his.

  • Ubuntu Codeblocks says Permission denied when I compile and run my program 2014-01-29

    Hi I installed Codeblocks from apt-get and I wrote a simple hello world program to test it. When I run my program a new terminal opens and says: sh: /home/david/addressoffile: Permission denied Press Enter to continue. Any idea why I keep getting it?

  • Ubuntu Using KDevelop to compile and run C++ programs 2015-03-24

    So, I am having a problem getting KDevelop to compile my program: #include <iostream> using namespace std; int main () { cout << "Hello, World!" << endl; return 0; } When attempting to compile with the command cc helloworld.cpp The

  • Ubuntu C compiling and running programs 2013-01-06

    everyone, I am trying to compile C programs, I used pico, saved the helloworld.c then in the terminal typed gcc helloworld.c -o helloworld I get no errors but then when I try to run it I type just helloworld and get prompted with bash: helloworld: co

  • Ubuntu How to compile and run C programs ? 2014-10-17

    I am new to Ubuntu I am having a little 'C' program with me //hello.c #include <stdio.h> void main() { printf("Hello From Ubuntu"); return ; } when I tried to compile it using "cc hello.c" I get following errors : hello.c:1:19: error: stdio.h:

  • I am having problems compiling and running this program 2014-11-17

    This is what i wrote: #ifndef EMPLOYEE_TYPE_FLAG #define EMPLOYEE_TYPE_FLAG #include <iostream> using namespace std; struct employee_type { char ssn[10]; employee_type() { ssn[0]=''; } bool operator > (employee_type &e) { return strncmp(

  • Ubuntu Compiling and Running C++ in gedit 2014-12-17

    I am tring to compile and run c++ programs in gedit but so far im having know luck. I know there is a way to do this in the terminal but to me this seems ridiculous because with Dev and other compilers all i have to do it go to the task bar,click com

  • Ubuntu Compiling and running straight from Gedit 2013-12-17

    I do most of my c++ coding in Gedit. I know there are certain plugins for it that allow you to do different things....is there a way to compile and run the program straight from gedit? I.e. without having to open terminal and g++ filename.cpp and the

  • Ubuntu Compile and run free pascal with gedit ? 2014-10-02

    I am trying to use Gedit to compile and run that program in terminal using the external tools plugin, there is already a tool to build C programs, how do i modify it to build pascal using fpc ? And other tool i need is that gedit run that program tha

  • Ubuntu How to compile and Run C progs in Eclipse 2014-10-27

    I've been using the gcc compiler so now I know how to compile and run C programs. Right now I've installed the eclipse-cdt plugin and I'm trying to run the "hello world" C program to test it. But I am not able to compile and run. I got the make file

  • How to compile and run C with Knoppix Live 2012-08-14

    I'm using a knoppix live cd (recommended by uni) to compile and run c programs off a usb drive - practice makes perfect. I get compile error messages when not done properly, warning messages, etc which is good. But when I try to run the program nothi

  • How to compile and run a boost python program??? 2012-07-12

    Hai friends, I have a question which is very fundamental.. After installing boost python and bjam from ubuntu 9.04 repositories i can't find a clear detailed description about compiling and running the boost python program from it's tutorials .. Can

  • How to compile and run Java codes in ubuntu 8.10 2012-08-21

    I am new to linux and would like to know how to compile and run the java codes on linux. Currently I have installed Ubuntu 8.10 so kindly help me to know what are the different packages required to perform the respective job and how can I continue my