Important stuff will be in red.
Basically, what you need is a simple text editor (I recommend the newest version of Notepad++ because it has syntax highlighting like your normal editor). You can put all your code into the text editor and save it as a .CS file, which will look something like this:
using System;
using System.Collections.Generic;
using System.Text;
class ClassName
{
static void Main()
{
}
}
You put all of your code in the Main() method because that's what runs when you start the program. You can add more namespace references (the "using ... " statements at the top) as needed, such as System.Drawing. You can add other methods in the class if you want, just like normal. You can even add new classes -- just make sure that the end-brace of one class is outside the starting-brace of the next class.
The following are 3 main commands that you will use to interact with the user:
- Console.Write(string text);
- This outputs the text in the console window.
- Console.WriteLine(string text);
- This outputs the text in the console window and also puts a line break after, so whatever comes next will be on a new line. I think this is more useful overall than Console.Write(...).
- Console.ReadLine();
- This gets user input from the user and reads in everything the user types in until he/she presses [ENTER].
- It also halts program execution until the user presses [ENTER], so it is quite useful for this to be the last line in your Main() method so that it will pause at the end of the program so you can read the output from your program before hitting [ENTER] to terminate it.
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe "ConsoleApp.cs"
pause
"ConsoleApp.exe"
C:\Windows\Microsoft.NET\Framework.
The next line tells the console to pause so you can see what happened during the compilation and so that you can fix any errors that were found.
The third line runs your application that is created if the compile was successful (or the last successful build if it failed). Again, change "ConsoleApp.cs" to the filename you chose for your C# source code file.
Run this .BAT file to compile and run your new console application!
You can download the entire bunch of files here (ConsoleApp.zip) including a template for your Console Application and the batch file to compile it. The numbers in the .BAT files represent the version of .NET that it's going to try to compile with. I recommend you try them from latest (v4.0.30319) to oldest (v2.0.50727) and use the newest .NET version that works. Remember to rename all instances of "ConsoleApp.cs" to whatever you want to name your C# source code file. Just another note: I like to name the compilers "c_ConsoleApp.bat" just so I know which .BAT file will compile which .CS file. It's not mandatory, but I find it helpful when there's a lot of files in one folder.
No comments:
Post a Comment