Dev Notes Dev Notes
  • Home
  • Categories
  • Tags
  • Archives

How to run C# app on Mac from command line?

Download and install Xamarin Studio: Xamarin Studio

Open terminal and check commands:

mcs
mono
###################
mcs - C# compiler
mono - Mono runtime

Console example

Create file:

vim monoapp.cs

using System;                 // Importing namespace

class Test                    // Class declaration
{
  static void Main()          // Method declaration
  {
    //int x = 12 * 30;          // Statement 1
    Console.WriteLine ("Hello form mono!");    // Statement 2
  }                           // End of method
}

Compile :

mcs monoapp.cs

And run:

mono monoapp.exe

Result:

Hello from mono!

WinForms UI example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class HelloWorld : Form
{
    static public void Main ()
    {
        Application.Run (new HelloWorld ());
    }

    public HelloWorld ()
    {
        Button b = new Button ();
        b.Text = "Click Me!";
        b.Click += new EventHandler (Button_Click);
        Controls.Add (b);
    }

    private void Button_Click (object sender, EventArgs e)
    {
        MessageBox.Show ("Button Clicked!");
    }
}

Compile and run:

mcs -pkg:dotnet  win_mono_app.cs

And run:

mono win_mono_app.exe

Sample UI

Links:

MonoDevelop

Xamarin Samples


Published

Dec 12, 2015

Last Updated

Dec 12, 2015

Category

HowTo

Tags

  • csarp 1
  • mac 1
  • mono 1
  • xamarin 1

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor