当前位置: 移动技术网 > IT编程>开发语言>c# > 深入Ref,Out的理解及其使用

深入Ref,Out的理解及其使用

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:    class program        
复制代码 代码如下:

    class program 
       { 
           //使用out后必须对变量赋值 
           public void testout(out int x, out int y) 
           { 
               x = 1; 
               y = 2; 
           } 
           //此时传进来的值分别为x1:10,y1:11,输出之后的x1的值为2 

           public void testref(ref int x, ref int y) 
           { 
               //引用剪剪那句话传进来的是猪,出来的可能是头牛(很精辟!) 
               x = 2; 

           } 
           static void main(string[] args) 
           { 
               int x; 
               int y; 
               program p1 = new program(); 
               p1.testout(out x,out y); 
               console.writeline("x={0},y={1}", x, y); 
               //在使用之前ref必须对变量赋值 
               int x1 = 10; 
               int y1 = 11; 
               p1.testref(ref x1,ref y1); 
               console.writeline("x1={0},y1={1}", x1, y1); 
           } 
       } 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网