Entradas

Mostrando entradas de enero, 2021

Compile and Execute C# on Ubuntu Linux

To explain how to compile and execute C# code on Ubuntu, we will  create a hello world method for adding two numbers and show the result after the hello world message. We create the file hello.cs with the following content:   using System; //Declare namespace namespace HelloWorldApp { //Declare HelloWorld class class HelloWorld { //Add method for adding two numbers public int Add(int a, int b) { return (a + b); } //Main method static void Main(string[] args) { //Print Hello World! message Console.WriteLine("Hello World!"); //Create HelloWorld class object HelloWorld hw = new HelloWorld(); //Calling Add method from the HelloWorld(hw) object, adding 1 + 2 int adding = hw.Add(1,2); //Prints the adding variable Console.WriteLine(adding); } } }    Install mono-complete package:   $ sudo apt install mono-complete Compile the hello.cs script, in order to generate the hello.exe file $ mcs -out:hello.exe hello.cs Execute the hello.e

Compilar y Ejecutar C# en Ubuntu Linux

Para explicar como compilar y ejecutar código C# en Ubuntu, haremos un hello world y un método para sumar dos números y mostrar el resultado de esa sumatoria después del mensaje de hello world. Creamos el archivo hello.cs con el siguiente contenido:   using System; //Se declara el namespace namespace HelloWorldApp { //Se declara la clase class HelloWorld { //Método Add para sumar dos números public int Add(int a, int b) { return (a + b); } //Método Main o Principal static void Main(string[] args) { //Se imprime mensaje Hello World! Console.WriteLine("Hello World!"); //Se crea objeto de la clase HelloWorld HelloWorld hw = new HelloWorld(); //Se invoca al método Add de la clase Hello World(hw), para sumar 1 + 2 int adding = hw.Add(1,2); //Se imprime la variable adding, que es el resultado de la sumatoria. Console.WriteLine(adding); } } }    Instalamos el paquete mono-complete:   $ sudo apt install mono-complete Compilamos el scri