C# static Main(字符串[] args)
C# static Main(string[] args)
您好,我正在尝试学习一些 C# 编程,但遇到了一些我很难理解的问题。我通常大部分时间都与 VB6、VB.NET 和 VBA 打交道,所以我对编程有一些了解。这是我不确定的...
假设您有两段代码...
1)
static int Area(int h, int w)
{
return h*w;
}
2)
static void Main(string[] args)
{
int res = Area(w: 5, h: 8);
Console.WriteLine(res);
}
所以在第一个片段中,我正在做 return,它执行 H * W 的乘法运算,但我们不像在 #2 中那样用 Console.WriteLine()
编写它?
return 的实际作用是什么?
#2 - 第一行 static void Main(string[] args)
- 特别是 string[] args 部分 - 这是什么意思?
return
returns 从函数到调用者的值,例如您可以将函数调用的结果分配给一个变量。
string[] args
是从命令行 运行 传递给程序的参数数组。
在这种情况下,return 传回答案,它将 h*w 返回给调用 area 方法的主要方法
String[] args 指的是可以传递给 main 方法的参数,在这种情况下是一个字符串数组,可以通过调用这个 main 方法的任何东西传入。
1) return
是一个关键字,用于确定 function/method 的 结果 。在您的例子中,该方法称为 Area
。 See Methods.
2) args
是一个字符串数组,它将包含您从命令行 运行 传递给程序的所有参数。 See Command-line arguments。示例:
foo.exe -a -b
在上述情况下,args
数组将是 ["-a", "-b"]
So in the first snippet, I'm doing the return which does the multiplication of H * W but we do not write it as we do in the #2 with console.writeline? What does the return actually do?
return
表示调用函数时 return 的内容。调用函数时必须提供两个参数 h*w
,否则将抛出编译器错误。这就像一个代数表达式;该函数表示无需显示逻辑,只需输入两个数字,功能就会作为函数中的 return 变量应用。使用您的示例:
int res = Area(w: 5, h: 8)
这里调用函数Area(w: 5, h: 8)
,w
变量设置为5
,h
设置为8
。然后回到我们的函数:
static int Area(int h, int w)
{
return h*w;
}
替换变量,你得到:
static int Area(int h, int w)
{
return 8*5;
}
因此,当您在控制台中登录时,res = Area(w: 5, h: 8)
给出 res
作为 40
,您应该不会感到惊讶。
In #2 - the first line static void Main(string[] args) - especially the string []args part - what does this mean?
string[] args
表示发送到 Main
函数的参数数组,一旦您将脚本编译成 .exe
,它就特别有用。在命令行中,您可以执行以下操作:
compiled.exe "My" "Strings"
并且该数组将包含两个值; args[0] == "My"
和 args[1] == "Strings"
。您可以遍历 args[]
数组来使用它们。在此脚本中,未使用 args
数组。
编辑评论更正。
您好,我正在尝试学习一些 C# 编程,但遇到了一些我很难理解的问题。我通常大部分时间都与 VB6、VB.NET 和 VBA 打交道,所以我对编程有一些了解。这是我不确定的...
假设您有两段代码...
1)
static int Area(int h, int w)
{
return h*w;
}
2)
static void Main(string[] args)
{
int res = Area(w: 5, h: 8);
Console.WriteLine(res);
}
所以在第一个片段中,我正在做 return,它执行 H * W 的乘法运算,但我们不像在 #2 中那样用 Console.WriteLine()
编写它?
return 的实际作用是什么?
#2 - 第一行 static void Main(string[] args)
- 特别是 string[] args 部分 - 这是什么意思?
return
returns 从函数到调用者的值,例如您可以将函数调用的结果分配给一个变量。string[] args
是从命令行 运行 传递给程序的参数数组。
在这种情况下,return 传回答案,它将 h*w 返回给调用 area 方法的主要方法
String[] args 指的是可以传递给 main 方法的参数,在这种情况下是一个字符串数组,可以通过调用这个 main 方法的任何东西传入。
1) return
是一个关键字,用于确定 function/method 的 结果 。在您的例子中,该方法称为 Area
。 See Methods.
2) args
是一个字符串数组,它将包含您从命令行 运行 传递给程序的所有参数。 See Command-line arguments。示例:
foo.exe -a -b
在上述情况下,args
数组将是 ["-a", "-b"]
So in the first snippet, I'm doing the return which does the multiplication of H * W but we do not write it as we do in the #2 with console.writeline? What does the return actually do?
return
表示调用函数时 return 的内容。调用函数时必须提供两个参数 h*w
,否则将抛出编译器错误。这就像一个代数表达式;该函数表示无需显示逻辑,只需输入两个数字,功能就会作为函数中的 return 变量应用。使用您的示例:
int res = Area(w: 5, h: 8)
这里调用函数Area(w: 5, h: 8)
,w
变量设置为5
,h
设置为8
。然后回到我们的函数:
static int Area(int h, int w)
{
return h*w;
}
替换变量,你得到:
static int Area(int h, int w)
{
return 8*5;
}
因此,当您在控制台中登录时,res = Area(w: 5, h: 8)
给出 res
作为 40
,您应该不会感到惊讶。
In #2 - the first line static void Main(string[] args) - especially the string []args part - what does this mean?
string[] args
表示发送到 Main
函数的参数数组,一旦您将脚本编译成 .exe
,它就特别有用。在命令行中,您可以执行以下操作:
compiled.exe "My" "Strings"
并且该数组将包含两个值; args[0] == "My"
和 args[1] == "Strings"
。您可以遍历 args[]
数组来使用它们。在此脚本中,未使用 args
数组。
编辑评论更正。