Csharp/C Sharp/Language Basics/Main — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:39, 26 мая 2010
Содержание
- 1 Deal with the arguments
- 2 Every console app starts with Main
- 3 Main Function: Multiple Mains
- 4 Return different value to the operating system based on the argument length
- 5 Static Main function
- 6 The Main Function
- 7 The Main Function:Command-Line Parameters
- 8 The Main Function:Returning an Int Status
- 9 This is a simple C# program
- 10 Using command-line arguments to initialize an array.
- 11 Variations on the Main() Method
Deal with the arguments
<source lang="csharp"> using System; class SayHello {
public static void Main(string[] args) { if (args.Length > 0) { foreach (string arg in args) { if (arg.Equals("/help")) Console.WriteLine("Run this program as follows: sayhello.exe [name1] "); else Console.WriteLine("Hello " + "{0}", arg); } } else Console.WriteLine("For help, run sayhello.exe /help"); }
}
</source>
Every console app starts with Main
<source lang="csharp"> /* Learning C# by Jesse Liberty Publisher: O"Reilly ISBN: 0596003765
- /
namespace NotePad { public class HelloWorld { // every console app starts with Main static void Main() { System.Console.WriteLine("Hello world!"); } } }
</source>
Main Function: Multiple Mains
<source lang="csharp">
// error using System; class Complex {
static int Main() { // test code here Console.WriteLine("Console: Passed"); return(0); }
} public class TheMainFunctionMultipleMains {
public static void Main(string[] args) { foreach (string arg in args) Console.WriteLine(arg); }
}
</source>
Return different value to the operating system based on the argument length
<source lang="csharp"> using System; class SayHello {
public static int Main(string[] args) { if (args.Length > 0) { foreach (string arg in args) { if (arg.Equals("/help")) { Console.WriteLine("Run this program as follows:" + "sayhello.exe [name1] "); return (1); } else Console.WriteLine("Hello " + "{0}", arg); } return (0); } else Console.WriteLine("For help, run sayhello.exe /help"); return (2); }
}
</source>
Static Main function
<source lang="csharp"> /* Learning C# by Jesse Liberty Publisher: O"Reilly ISBN: 0596003765
- /
using System; namespace StaticTester { // create the class public class StaticTester { // Run is an instance method public void Run() { Console.WriteLine("Hello world"); } // Main is static static void Main() { // create an instance StaticTester t = new StaticTester(); // invoke the instance method t.Run(); } } } </source>
The Main Function
<source lang="csharp"> using System; public class TheMainFunction {
public static void Main() { Console.WriteLine("Hello, Universe!"); }
}
</source>
The Main Function:Command-Line Parameters
<source lang="csharp">
using System; public class CommandLineParameters {
public static void Main(string[] args) { foreach (string arg in args) Console.WriteLine("Arg: {0}", arg); }
}
</source>
The Main Function:Returning an Int Status
<source lang="csharp">
using System; public class ReturninganIntStatus {
public static int Main() { Console.WriteLine("Hello, Universe!"); return(0); }
}
</source>
This is a simple C# program
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
/*
This is a simple C# program. Call this program Example.cs.
- /
using System;
public class ExampleWriteLine {
// A C# program begins with a call to Main(). public static void Main() { Console.WriteLine("A simple C# program."); }
}
</source>
Using command-line arguments to initialize an array.
<source lang="csharp"> using System; public class InitArray {
public static void Main( string[] args ) { int arrayLength = Convert.ToInt32( args[ 0 ] ); int[] array = new int[ arrayLength ]; // create array int initialValue = Convert.ToInt32( args[ 1 ] ); int increment = Convert.ToInt32( args[ 2 ] ); for ( int counter = 0; counter < array.Length; counter++ ) array[ counter ] = initialValue + increment * counter; Console.WriteLine( "{0}{1,8}", "Index", "Value" ); for ( int counter = 0; counter < array.Length; counter++ ) Console.WriteLine( "{0,5}{1,8}", counter, array[ counter ] ); }
}
</source>
Variations on the Main() Method
<source lang="csharp"> /* The previous iteration of Main() was defined to take a single parameter (an array of strings) and return an integer data type. This is not the only possible form of Main(), however. It is permissible to construct your application¡�s entry point using any of the following signatures (assuming it is contained within a C# class or structure definition): // No return type, array of strings as argument. public static void Main(string[] args) { } // No return type, no arguments. public static void Main() { } // Integer return type, no arguments. public static int Main() { }
- /
</source>