Csharp/C Sharp/Language Basics/Main
Версия от 15:31, 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
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");
}
}
Every console app starts with Main
/*
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!");
}
}
}
Main Function: Multiple Mains
// 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);
}
}
Return different value to the operating system based on the argument length
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);
}
}
Static Main function
/*
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();
}
}
}
The Main Function
using System;
public class TheMainFunction
{
public static void Main()
{
Console.WriteLine("Hello, Universe!");
}
}
The Main Function:Command-Line Parameters
using System;
public class CommandLineParameters
{
public static void Main(string[] args)
{
foreach (string arg in args)
Console.WriteLine("Arg: {0}", arg);
}
}
The Main Function:Returning an Int Status
using System;
public class ReturninganIntStatus
{
public static int Main()
{
Console.WriteLine("Hello, Universe!");
return(0);
}
}
This is a simple C# program
/*
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.");
}
}
Using command-line arguments to initialize an array.
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 ] );
}
}
Variations on the Main() Method
/*
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()
{
}
*/