[edit] C# Main() Function
Functions are able to accept and return parameters. Main() is no different than any other function. The C# Main function is slightly different in that it is able to accept and return values to external applications. Such as MS word or any other external application. The are four versions of the Main() function:
'C# Main() Versions:
- static void Main() - accepts no parameters and returns no values
- static void Main(string[] args) - returns no parameters and accepts a string array named args
- static int Main() - returns an int value and accepts to parameters
- static int Main(string[] args) - returns an int and accepts a string array named args
Lets accept parameters from an external program like DOS. Here are the instructions to follow:
using System;
class Program
{
static void Main(string[] args)
{
foreach(string x in args)
Console.WriteLine(x);
Console.Read();
}
}
Compile and make an executable from the code above. When you finish compiling the code move the executable file to the C:\ directory and rename the file to test.exe. Then run run a DOS window and move to the c:\ directory.
C:\>test.exe it's working 1 2 3 123
Each space separates each parameter. The output should look like this:
The C# program accepted the arguments and simply displayed the arguments located in an array.
|