Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: FAQ: Compiling and Running C# Programs

  1. #1
    Join Date
    Apr 2007
    Beans
    14,781

    FAQ: Compiling and Running C# Programs

    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 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.

    Code:
    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
    PHP Code:
    //First C# Program
    using System;

    namespace 
    Main
    {
       class 
    Welcome
       
    {
          public static 
    void Mainstring[] 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:
    Code:
    gmcs Main.cs
    If all went fine, nothing is printed and you get back to the shell. Now, run it:
    Code:
    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:
    Code:
    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 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...
    Last edited by LaRoza; July 23rd, 2008 at 08:35 PM.

  2. #2
    Join Date
    Sep 2007
    Beans
    Hidden!

    Re: FAQ: Compiling and Running C# Programs

    Good Thread,this will help a lot of windows c# programmers(new comers) and Linux users that want to learn c# and are facing problems compiling,setting up an environment,etc.
    Well Done.

  3. #3
    Join Date
    Apr 2007
    Beans
    14,781

    Re: FAQ: Compiling and Running C# Programs

    Quote Originally Posted by Kadrus View Post
    Good Thread,this will help a lot of windows c# programmers(new comers) and Linux users that want to learn c# and are facing problems compiling,setting up an environment,etc.
    Well Done.
    Thanks. You'll notice it is a rip off of the Java FAQ, which was a rip off of the C++ FAQ (which I didn't write).

    I just changed a few names and commands, added a few sentences and it was done

  4. #4
    Join Date
    Sep 2007
    Beans
    Hidden!

    Re: FAQ: Compiling and Running C# Programs

    Well still useful ,although I don't code with C# .But do some coding with F#.

  5. #5
    Join Date
    Oct 2005
    Location
    Seattle, WA
    Beans
    494
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: FAQ: Compiling and Running C# Programs

    Quote Originally Posted by LaRoza View Post
    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.
    Very well said. You should link to this in a sticky or sig.
    www.runtime-era.com - Blog About My Journey as a Developer

  6. #6
    Join Date
    Apr 2007
    Beans
    14,781

    Re: FAQ: Compiling and Running C# Programs

    Quote Originally Posted by era86 View Post
    Very well said. You should link to this in a sticky or sig.
    I didn't actually write it: http://ubuntuforums.org/showthread.php?t=689635

  7. #7
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: FAQ: Compiling and Running C# Programs

    I'm not a C# expert, but:

    PHP Code:
    //First C# Program
    using System;

    namespace 
    Main
    // <------ Where's the matching brace?
       
    class Welcome
       
    {
          public static 
    void Mainstring[] args )
          {
              
    Console.Write("Hello world\n");
          }
       } 
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  8. #8
    Join Date
    Apr 2007
    Beans
    14,781

    Re: FAQ: Compiling and Running C# Programs

    Quote Originally Posted by Can+~ View Post
    I'm not a C# expert, but:
    Think you are clever, huh?

    Adding brace and counter snipe comment

  9. #9
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: FAQ: Compiling and Running C# Programs

    Quote Originally Posted by LaRoza View Post
    Think you are clever, huh?

    Adding brace and counter snipe comment
    No, actually I know crap about C#. I even though that you left it open because it was part of the notation.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  10. #10
    Join Date
    Apr 2007
    Beans
    14,781

    Re: FAQ: Compiling and Running C# Programs

    Quote Originally Posted by Can+~ View Post
    No, actually I know crap about C#. I even though that you left it open because it was part of the notation.
    Think Java (if you don't know Java, think C++).

    As for it being part of the notation, it is technically, but if you want it to compile you should close it.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •