学习笔记一:
Main()
和命令行参数:来自微软MSDN
1.Main()
和命令行参数(C# 编程指南)
Main
方法是程序的入口点,您将在那里创建对象和调用其他方法。一个 C# 程序中只能有一个入口点。
C#
class
TestClass
{
static void Main(string[] args)
{
// Display the number of command line arguments:
System.Console.WriteLine(args.Length);
}
}
概述
- Main方法是程序的入口点,程序控制在该方法中开始和结束。
- 该方法在类或结构的内部声明。它必须为静态方法,而不应为公共方法。(在上面的示例中,它接受默认访问级别 private。)
- 它可以具有 void 或 int 返回类型。
- 声明 Main 方法时既可以使用参数,也可以不使用参数。
- 参数可以作为从零开始索引的命令行参数来读取。
- 与 C 和 C++ 不同,程序的名称不会被当作第一个命令行参数。
2
如何:使用
foreach
访问命令行参数(
C#
编程指南)
下面的示例演示如何使用 foreach 输出命令行参数。
C#
// arguments: John Paul Mary
C#
class CommandLine2
{
static void Main(string[] args)
{
System.Console.WriteLine("Number of command line parameters = {0}", args.Length);
foreach (string s in args)
{
System.Console.WriteLine(s);
}
}
}
输出
Number of command line parameters = 3
John
Paul
Mary
3.
如何:显示命令行参数(
C#
编程指南)
可以通过
Main 的可选参数来访问通过命令行提供给可执行文件的参数。参数以字符串数组的形式提供。数组的每个元素都包含一个参数。参数之间的空白被移除。例如,下面是对一个假想的可执行文件的命令行调用:
本示例显示了传递给命令行应用程序的命令行参数。显示的输出对应于上表中的第一项。
C#
class CommandLine
{
static void Main(string[] args)
{
// The Length property provides the number of array elements
System.Console.WriteLine("parameter count = {0}", args.Length);
for (int i = 0; i < args.Length; i++)
{
System.Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
}
输出
parameter count = 3
Arg[0] = [a]
Arg[1] = [b]
Arg[2] = [c]
4.Main() 返回值(C# 编程指南)
Main 方法可以是
void 类型:
C#
static void Main()
{
//...
}
它还可以返回
int:
static int Main()
{
//...
return 0;
}
如果不需要使用
Main 的返回值,则返回
void 可以使代码变得略微简单。但是,返回整数可使程序将状态信息与调用该可执行文件的其他程序或脚本相关。下面的示例演示使用
Main 的返回值。
示例
在此示例中,使用了一个批处理文件来执行程序并测试
Main 函数的返回值。在 Windows 中执行程序时,
Main 函数返回的任何值都将存储在名为
ERRORLEVEL 的环境变量中。通过检查
ERRORLEVEL 变量,批处理文件可以确定执行的结果。通常,返回值为零指示执行成功。下面是一个非常简单的程序,其
Main 函数返回零。
C#
class MainReturnValTest
{
static int Main()
{
//...
return 0;
}
}
由于此示例使用了批处理文件,因此最好从命令行编译这段代码,如
如何:设置环境变量中所示。
然后,使用批处理文件调用前面的代码示例所生成的可执行文件。由于代码返回零,因此批处理文件将报告成功。但如果前面的代码更改为返回非零值,然后重新编译,则批处理文件的后续执行将指示失败。
rem test.bat
@echo off
MainReturnValueTest
@if "%ERRORLEVEL%" == "0" goto good
:fail
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end
:good
echo Execution Succeded
echo return value = %ERRORLEVEL%
goto end
:end
示例输出
Execution Succeded
return value = 0
5.
命令行参数(
C#
编程指南)
Main 方法可以使用参数,在这种情况下它采用下列形式之一:
C#
static int Main(string[] args)
C#
static void Main(string[] args)
Main 方法的参数是表示命令行参数的
String 数组。通常通过测试
Length 属性来检查参数是否存在,例如:
C#
if (args.Length == 0)
{
System.Console.WriteLine("Please enter a numeric argument.");
return 1;
}
还可以使用
Convert 类或
Parse 方法将字符串参数转换为数值类型。例如,下面的语句使用
Int64 类上的
Parse 方法将字符串转换为 long 型数字:
long num = Int64.Parse(args[0]);
也可以使用别名为
Int64 的 C# 类型
long:
long num = long.Parse(args[0]);
还可以使用
Convert 类的方法
ToInt64 完成同样的工作:
long num = Convert.ToInt64(s);
示例
在此示例中,程序在运行时采用一个参数,将该参数转换为整数,并计算该数的阶乘。如果没有提供参数,则程序发出一条消息来解释程序的正确用法。
C#
// arguments: 3
C#
public class Functions
{
public static long Factorial(int n)
{
if (n < 0) { return -1; } //error result - undefined
if (n > 256) { return -2; } //error result - input is too big
if (n == 0) { return 1; }
// Calculate the factorial iteratively rather than recursively:
long tempResult = 1;
for (int i = 1; i <= n; i++)
{
tempResult *= i;
}
return tempResult;
}
}
C#
class MainClass
{
static int Main(string[] args)
{
// Test if input arguments were supplied:
if (args.Length == 0)
{
System.Console.WriteLine("Please enter a numeric argument.");
System.Console.WriteLine("Usage: Factorial <num>");
return 1;
}
try
{
// Convert the input arguments to numbers:
int num = int.Parse(args[0]);
System.Console.WriteLine("The Factorial of {0} is {1}.", num, Functions.Factorial(num));
return 0;
}
catch (System.FormatException)
{
System.Console.WriteLine("Please enter a numeric argument.");
System.Console.WriteLine("Usage: Factorial <num>");
return 1;
}
}
}
输出
The Factorial of 3 is 6.
注释
下面是该程序的两个运行示例(假定程序名为
Factorial.exe)。
运行示例 #1:
输入下面的命令行:
Factorial 10
您将获得下面的结果:
The Factorial of 10 is 3628800.
运行示例 #2:
输入下面的命令行:
Factorial
您将获得下面的结果:
Please enter a numeric argument.
Usage: Factorial <num>