They gave me this code as homework, but I don't know how to run it, any help?
using System;
public class Example
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}
Another possible solution if you just want to see how the execution of that code behaves is to do it somewhere where you can execute code online.
There are many sites like:
In all of them you can run and share your code.
In this case I have chosen the first one because you can simulate the input of the program and in this way you see how it would behave with the arguments.
Here is your example
Hope this can help you.
To run C# programs there is nothing like Visual Studio that has the full support of Microsoft (Although MonoDevelop also exists ).
You can download here the community version that is free for students
Once you have downloaded and installed Visual Studio, you can create a project
Or if you download it in Spanish
And we choose the option on the left side
Visual C#
->Windows Desktop
and finallyConsole Application
Once finished it will create a basic project structure and a start file, like this
We can delete the File
Program.cs
so that it does not cause noise and to add your code we will only do the following.In the tools menu we select
Project
->Add Class
(or in SpanishProyecto
->Agregar clase
)In the next Popup we will only change the name to
Example.cs
(Although C# has no limitations by filename, it is good practice that it is called the same as your class)This will generate this code
Now we will replace class Example with your code so that it looks like this
Now there is a problem in your code , the variable is not declared, the
i
easiest thing is to add aint
before in the cyclefor
so that it looks like thisTo execute the program we will press the button
Start
orIniciar
Finally , what your program does is print values that are passed as arguments from the console.
When it is executed from Visual Studio, the console will only be shown for a few seconds, since by default no arguments are passed in the execution and it closes automatically when the program ends.
We need to find the created .exe file and run it from cmd
By default it will be the
mis documentos\visual studio 2015\Projects\<Nombre de tu proyecto>\<Nombre de tu proyecto>\bin\Debug
Once in the correct folder we will execute in the cmd
Which should show something like
Assuming you don't have the NET framework in your system PATH (which is the default), you can compile from the console as follows:
c:\prueba\programa.cs
You position yourself in the NET framework installation directory where the csc.exe compiler is located (vX.X.XXXXX is the version you have installed):
To compile:
The result will be in the current directory.
To run the created executable:
If you have the NET framework in the PATH, then you can compile and run from any location:
By the way, the code has a syntax error, the for should be:
For more information I leave you the following link that discusses ways to compile your application in C # with or without IDE:
Build apps in Visual Studio
In
OSX
you can installmono
, which is useful when compiling filesc#
and executing them later.For education issues you can use Visual Studio Community , it is free and it will be useful for everything you are going to work with .NET technologies, there you select a new console application and paste your code, as help here you will find more details How to create a console?