通过out 关键字实现方法的第二个返回值

以下为百度知道内容

下面是一个使用out形参例子。方法RectInfo()返回已知边长的矩形面积。在形参isSquare中,如果矩形是正方形,返回true,否则返回false。因此,RectInfo()向调用者返回两条信息

using  System;
class  Rectangle{
     int  side1;
     int  side2;
     public  Rectangle( int  i,  int  j){
         side1 = i;
         side2 = j;
     }
     // Return area and determine if square.
     public  int  RectInfo( out  bool  isSquare){
         if  (side1 == side2) 
             isSquare =  true ;
         else 
             isSquare =  false ;
         return  side1 * side2;
     }
}
  
class  OutDemo{
     static  void  Main(){
         Rectangle rect =  new  Rectangle(10, 23);
         int  area;
         bool  isSqr;
         area = rect.RectInfo( out  isSqr);
         if  (isSqr) 
             Console.WriteLine( "rect is a square." );
         else 
             Console.WriteLine( "rect is not a square." );
         Console.WriteLine( "Its area is "  + area +  "." );
         Console.ReadKey();
     }
}

猜你喜欢

转载自blog.csdn.net/anyqu/article/details/80218326