深入.NET学习[一]

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
修饰的参数必须为一维数组,事实上通常就是以群集方式来实现多个或者任意多个参数的控制的,所以数组是最简单的选择; param 修饰的参数数组,可是是任何类型。因此,如果需要接受任何类型的参数时,只要设置数组类型为 object 即可; param 必须在参数列表的最后一个,并且只能使用一次。

For example:

 

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 

 

3、关于ref 和 out.

.NET中以操作符 ref 和 out 来标识值类型按照应用类型方式传递 。其区别在于:

    ref 在参数传递前必须初始化;

    out 则在传递前不必初始化,但是 在传递时 必须显式赋值。

 

  • 引用类型参数的按值传递,传递的是参数本身的值,也就是上面提到的对象的引用;
按引用传递,传递的不是参数本身的值,而是参数的地址。如果参数为值类型,则传递的是该值类型的地址;如果参数为引用类型,则传递的是对象引用的地址。

 举例说明

1,按值 类型传参。传递的是参数本身(也就是指针地址)。

 

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
        }  
    }
}

 

 

2,使用关键字 ref / out 按 引用传值。

   refout 关键字将告诉编译器,方法传递的是参数地址(也就是实例的指针),而不是参数本身。

 
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
        }            
    }
}

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/softwareflying/archive/2010/03/17/1688270.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值