认清ref和out,C#

ref和out都是参数修饰符

他们共同作用是修改一个变量

ref使用前必须要进行初始化,out使用时可以不用初始化

修饰方法用static最好

使用时需要加关键字,ref a,out b

 

class t1{

  public static void tmp(ref int a,out int b){

  a = 10;

  b = 11;

  }

  public static void Main(string[] args){

  int a=0;  //使用时必须先赋值,否则会报错"使用了为赋值的参数"

  int b;  //使用out的时候可以不用给方法初始化

  tmp(a,b);  //a为10,b为11

  }

}

posted on 2019-12-25 12:40  Robot1011  阅读(165)  评论(0)    收藏  举报

导航