1,static void Main(string[] args){} 其中 args 表示命令行的参数
解析
Main(string [] args),Main函数的参数可以为空,也可以为string数组类,其作用是接受命令行参数,例如在命令行下运行程序时,args提供了输入命令行参数的入口。2,可变数目参数 ,parm.
param
关键字的实质:param是定制特性ParamArrayAttribute的缩写.static void ShowAgeSum(string team, params int[] ages){...}
实质
static void ShowAgeSum(string team, [ParamArrayAttribute] int[] ages){...}
Param 学要注意的:
- param
For example:
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace GetType
7 {
8 class Program
9 {
10 static void Main()
11 {
12 showNumSum("liuhuan",22,12,32);
13
14 showNumSum("lisa",1,3,456,76,87,54,34,54);
15 Console.ReadKey();
16
17 }
18 static void showNumSum(string team,int i,int j,int k)
19 {
20 Console.WriteLine("The no parm team {0}'s age is {1}",team,i+j+k);
21 }
22 static void showNumSum(string team, params int[] ages)
23 {
24 int sum = 0;
25 for (int i &#61; 0; i < ages.Length; i&#43;&#43;)
26 {
27 sum &#43;&#61; ages[i];
28 }
29 Console.WriteLine("The parm team {0}&#39;s age is {1}", team, sum);
30 }
31 }
32 }
33
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace GetType
7 {
8 class Program
9 {
10 static void Main()
11 {
12 showNumSum("liuhuan",22,12,32);
13
14 showNumSum("lisa",1,3,456,76,87,54,34,54);
15 Console.ReadKey();
16
17 }
18 static void showNumSum(string team,int i,int j,int k)
19 {
20 Console.WriteLine("The no parm team {0}&#39;s age is {1}",team,i&#43;j&#43;k);
21 }
22 static void showNumSum(string team, params int[] ages)
23 {
24 int sum &#61; 0;
25 for (int i &#61; 0; i < ages.Length; i&#43;&#43;)
26 {
27 sum &#43;&#61; ages[i];
28 }
29 Console.WriteLine("The parm team {0}&#39;s age is {1}", team, sum);
30 }
31 }
32 }
33
3、关于ref 和 out.
.NET中以操作符 ref 和 out 来标识值类型按照应用类型方式传递 。其区别在于&#xff1a;
ref 在参数传递前必须初始化&#xff1b;
out 则在传递前不必初始化&#xff0c;但是 在传递时 必须显式赋值。
- 引用类型参数的按值传递&#xff0c;传递的是参数本身的值&#xff0c;也就是上面提到的对象的引用&#xff1b;
1,按值 类型传参。传递的是参数本身&#xff08;也就是指针地址&#xff09;。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetType
{
class Program
{
static void Main()
{
string str &#61; "Old String";
ChangeStr(str);
Console.WriteLine(str); //Displays Old String.
Console.ReadKey();
}
static void ChangeStr(string aStr)
{
aStr &#61; "Changing String";
Console.WriteLine(aStr); //Displays Changing String
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetType
{
class Program
{
static void Main()
{
string str &#61; "Old String";
ChangeStr(str);
Console.WriteLine(str); //Displays Old String.
Console.ReadKey();
}
static void ChangeStr(string aStr)
{
aStr &#61; "Changing String";
Console.WriteLine(aStr); //Displays Changing String
}
}
}
2&#xff0c;使用关键字 ref / out 按 引用传值。
ref和out关键字将告诉编译器&#xff0c;方法传递的是参数地址(也就是实例的指针)&#xff0c;而不是参数本身。代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetType
{
class Program
{
static void Main()
{
string str &#61; "Old String";
//use the keyword ref
ChangeStr(ref str);
Console.WriteLine(str); //Displays Changing String.
Console.ReadKey();
}
static void ChangeStr(ref string aStr)
{
aStr &#61; "Changing String";
Console.WriteLine(aStr); //Displays Changing String
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetType
{
class Program
{
static void Main()
{
string str &#61; "Old String";
//use the keyword ref
ChangeStr(ref str);
Console.WriteLine(str); //Displays Changing String.
Console.ReadKey();
}
static void ChangeStr(ref string aStr)
{
aStr &#61; "Changing String";
Console.WriteLine(aStr); //Displays Changing String
}
}
}