Csharp/CSharp Tutorial/Language Basics/Main — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:19, 26 мая 2010

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);
        }
    }