关于Ref和Out的区别,附上小例子

来源:互联网 发布:java soa的理解 编辑:程序博客网 时间:2024/06/02 01:32

 

[c-sharp]viewplaincopyprint?
  1. class Program  
  2.     
  3.        //使用out后必须对变量赋值     
  4.        public void TestOut(out int x, out int y)  
  5.         
  6.            1;  
  7.            2;  
  8.         
  9.        //此时传进来的值分别为x1:10,y1:11,输出之后的x1的值为2     
  10.   
  11.        public void TestRef(ref int x, ref int y)  
  12.         
  13.            //引用剪剪那句话传进来的是猪,出来的可能是头牛(很精辟!)     
  14.            2;  
  15.           //这边可以不对y赋值,而out那就不行了.  
  16.   
  17.         
  18.        static void Main(string[] args)  
  19.         
  20.            int x=10;  
  21.            int y=11;  
  22.            Program P1 new Program();  
  23.            P1.TestOut(out x, out y); //out会清空原来变量的值  
  24.            Console.WriteLine("x={0},y={1}"x, y);  
  25.            //在使用之前ref必须对变量赋值     
  26.            int x1 10;  
  27.            int Y1 11;  
  28.            P1.TestRef(ref x1, ref Y1);  
  29.            Console.WriteLine("x1={0},y1={1}"x1, Y1);  
  30.            Console.ReadKey();  
  31.         
  32.   
  33.        //el可以把参数的数值传递进函数,  
  34.        //但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,  
  35.        //所以你必须初始化一次。这个就是两个的区别,或者说就像有的网友说的,rel是有进有出,out是只出不进。  
  36.        
  37.   
  38.       

 

以前不知道ref和out有个啥用,其实ref和out可以在不需要返回值的情况下,传递值,

在有返回值的方法里,除了可以得到返回的值外,还可以得到方法里面声明的ref,和out参数的值

 

比喻如下的一个验证方法,可以传递验证的信息,既返回了真假,又可以得到验证的信息

 

[c-sharp]viewplaincopyprint?
  1. /// <summary>  
  2. ///检查表单验证信息  
  3. /// </summary>  
  4. private bool CheckFromInfo(ref string name)  
  5.  
  6.   
  7.     if (txt_leaveDate.Text == "" 
  8.      
  9.         name="请假日期不能为空" 
  10.         return false 
  11.      
  12.     if (txt_name.Text == "" 
  13.      
  14.         name "姓名不能为空" 
  15.         return false 
  16.      
  17.     if (txt_writedate.Text == "" 
  18.      
  19.         name="申请日期不能为空" 
  20.         return false 
  21.   
  22.      
  23.     string enddate txt_leaveDate.Text;  
  24.     DateTime date Convert.ToDateTime(enddate);  
  25.     DateTime now =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));  
  26.     if (date now)   
  27.      
  28.         name "请假日期不能小于申请日期(填表日期)!" 
  29.         return false 
  30.      
  31.     if (ddltype.SelectedItem.Text == "请选择"  
  32.      
  33.         name "请选择请假类型!" 
  34.         return false 
  35.      
  36.     return true 
  37.    
  38.        
  39.     
  40.            
  41.      
  42.  
  43.   
  44.   
  45.   
  46. 是调用  
  47.      string name=null 
  48.     if (CheckFromInfo(ref name) == false 
  49.      
  50.   
  51.         JScript.Alert(name,this.Page);//弹出提示消息  
  52.         return 
  53.      

 

 上面要是用out的话呢?在离开方法CheckFromInfo()之前.必须在方法里面为name赋值,你要保证在此方法出来的时候.

name是有值的.不然人家叫out干吗?out嘛.你不让人家带东西出来,不给面试,人家当然不乐意了

用ref的话,就不必了.如果在方法里面refname,name的值 没有改变的话呢.那么你在CheckFromInfo()方法外边申明的值就是它最终的值了,而out你在外边不论声明什么值,它都会清空的,因为它要out嘛,要带自己的东西出来...

0 0
原创粉丝点击