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:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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 = 0 ; i < ages.Length; i ++ )
26 {
27 sum += ages[i];
28 }
29 Console.WriteLine( " The parm team {0}'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}'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 = 0 ; i < ages.Length; i ++ )
26 {
27 sum += ages[i];
28 }
29 Console.WriteLine( " The parm team {0}'s age is {1} " , team, sum);
30 }
31 }
32 }
33
3、关于ref 和 out.
.NET中以操作符 ref 和 out 来标识值类型按照应用类型方式传递 。其区别在于:
ref 在参数传递前必须初始化;
out 则在传递前不必初始化,但是 在传递时 必须显式赋值。
- 引用类型参数的按值传递,传递的是参数本身的值,也就是上面提到的对象的引用;
1,按值 类型传参。传递的是参数本身(也就是指针地址)。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetType
{
class Program
{
static void Main()
{
string str = " Old String " ;
ChangeStr(str);
Console.WriteLine(str); // Displays Old String.
Console.ReadKey();
}
static void ChangeStr( string aStr)
{
aStr = " 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 = " Old String " ;
ChangeStr(str);
Console.WriteLine(str); // Displays Old String.
Console.ReadKey();
}
static void ChangeStr( string aStr)
{
aStr = " Changing String " ;
Console.WriteLine(aStr); // Displays Changing String
}
}
}
2,使用关键字 ref / out 按 引用传值。
ref 和 out 关键字将告诉编译器,方法传递的是参数地址(也就是实例的指针),而不是参数本身。![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetType
{
class Program
{
static void Main()
{
string str = " Old String " ;
// use the keyword ref
ChangeStr( ref str);
Console.WriteLine(str); // Displays Changing String.
Console.ReadKey();
}
static void ChangeStr( ref string aStr)
{
aStr = " 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 = " Old String " ;
// use the keyword ref
ChangeStr( ref str);
Console.WriteLine(str); // Displays Changing String.
Console.ReadKey();
}
static void ChangeStr( ref string aStr)
{
aStr = " Changing String " ;
Console.WriteLine(aStr); // Displays Changing String
}
}
}