Csharp/CSharp Tutorial/Language Basics/Main
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
- 1 Looking for Command-Line Options
- 2 Looking for Command-Line Options (Simplified)
- 3 Main Entry Point
- 4 The Main Function
- 5 The main function: returning an int status
- 6 The Main Function: use foreach to loop through command-line parameters
- 7 Using foreach to loop through the parameter from Main function
Looking for Command-Line Options
class MainClass
{
static void Main(string[] args)
{
if(args[0][0]=="-")
{
//This parameter is an option
}
}
}
Looking for Command-Line Options (Simplified)
class MainClass
{
static void Main(string[] args)
{
string arg = args[0];
if(arg[0] == "-")
{
//This parameter is an option
}
}
}
Main Entry Point
Main is the entry point method for a C# application.
static void Main() {
// main block
}
static int Main() {
// main block
}
static void Main(string [] args){
// main block
}
static int Main(string [] args){
//
}
The Main Function
All C# applications begin execution by calling Main( ).
using System;
class MainClass
{
public static void Main()
{
Console.WriteLine("Hello, Universe!");
}
}
Hello, Universe!
The main function: returning an int status
using System;
class MainClass
{
public static int Main()
{
Console.WriteLine("Hello, Universe!");
return(0);
}
}
Hello, Universe!
The Main Function: use foreach to loop through command-line parameters
using System;
class MainClass
{
public static void Main(string[] args)
{
foreach (string arg in args)
Console.WriteLine("Arg: {0}", arg);
}
}
Using foreach to loop through the parameter from Main function
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0} command line arguments were specified:",args.Length);
foreach (string arg in args)
Console.WriteLine(arg);
}
}