CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

传出参数out

楼主gxhmsy(1加1)2005-11-18 17:35:42 在 .NET技术 / C# 提问

 
  using   System;  
  class   Method   {  
          public   static   void   OutParam(out   int   o)  
          {  
            o=100;  
        }  
        public   static   int   Main()   {  
            int   x;  
            OutParam(out   x);  
            Console.WriteLine("这是   OutParam之后的x={0}",x);  
            return   0;  
        }  
  }  
  调用OutParam(out   x);后那o的值就会传给x,这个是怎   么实现的?不是太理解哦。 问题点数:20、回复次数:4Top

1 楼yezie(椰子)(.Net)回复于 2005-11-18 17:38:15 得分 0

你可以看成没outTop

2 楼ld_thinking(懒得想)回复于 2005-11-18 17:40:16 得分 0

int   &   oTop

3 楼Macosx(结贴)回复于 2005-11-18 17:53:22 得分 5

Read   MSDN  
   
   
  The   out   keyword   causes   arguments   to   be   passed   by   reference.   This   is   similar   to   the   ref   keyword,   except   that   ref   requires   that   the   variable   be   initialized   before   being   passed.   To   use   an   out   parameter,   both   the   method   definition   and   the   calling   method   must   explicitly   use   the   out   keyword.   For   example:  
   
      Copy   Code    
  class   OutExample  
  {  
          static   void   Method(out   int   i)  
          {  
                  i   =   44;  
          }  
          static   void   Main()  
          {  
                  int   value;  
                  Method(out   value);  
                  //   value   is   now   44  
          }  
  }  
     
   
  Although   variables   passed   as   an   out   arguments   need   not   be   initialized   prior   to   being   passed,   the   calling   method   is   required   to   assign   a   value   before   the   method   returns.  
   
  The   ref   and   out   keywords   are   treated   differently   at   run-time,   but   they   are   treated   the   same   at   compile   time.   Therefore   methods   cannot   be   overloaded   if   one   method   takes   a   ref   argument   and   the   other   takes   an   out   argument.   These   two   methods,   for   example,   are   identical   in   terms   of   compilation,   so   this   code   will   not   compile:  
   
      Copy   Code    
  class   CS0663_Example    
  {  
          //   compiler   error   CS0663:   "cannot   define   overloaded    
          //   methods   that   differ   only   on   ref   and   out"  
          public   void   SampleMethod(out   int   i)   {     }  
          public   void   SampleMethod(ref   int   i)   {     }  
  }  
     
   
  Overloading   can   be   done,   however,   if   one   method   takes   a   ref   or   out   argument   and   the   other   uses   neither,   like   this:  
   
      Copy   Code    
  class   RefOutOverloadExample  
  {  
          public   void   SampleMethod(int   i)   {     }  
          public   void   SampleMethod(out   int   i)   {     }  
  }  
     
   
  Remarks  
  Properties   are   not   variables   and   therefore   cannot   be   passed   as   out   parameters.  
   
  For   information   on   passing   arrays,   see   Passing   Arrays   Using   ref   and   out.  
   
  Example  
  Declaring   an   out   method   is   useful   when   you   want   a   method   to   return   multiple   values.   A   method   that   uses   an   out   parameter   can   still   a   variables   as   a   return   type   (see   return)   but   it   can   also   return   one   or   more   objects   to   a   calling   method   as   out   parameters.   This   example   uses   out   to   return   three   variables   with   a   single   method   call.   Note   that   the   third   argument   is   assigned   to   null.   This   allows   methods   to   return   values   optionally.  
   
      Copy   Code    
  class   OutReturnExample  
  {  
          static   void   Method(out   int   i,   out   string   s1,   out   string   s2)  
          {  
                  i   =   44;  
                  s1   =   "I've   been   returned";  
                  s2   =   null;  
          }  
          static   void   Main()  
          {  
                  int   value;  
                  string   str1,   str2;  
                  Method(out   value,   out   str1,   out   str2);  
                  //   value   is   now   44  
                  //   str1   is   now   "I've   been   returned"  
                  //   str2   is   (still)   null;  
          }  
  }  
     
   
  Top

4 楼happycharles()回复于 2005-11-18 17:53:49 得分 15

public   static   int   Main()   {  
  int   x;  
  OutParam(out   x);  
  return   0;  
  }  
   
  这个时Main,然后调OutParam()   传入   x,  
   
  OutParam(out   int   o)里给o赋了个值o=100,也就相当于给x赋值了,然后就没什么可说得了Top

相关问题

  • ado存储过程控件怎么得到传出的参数?
  • javascript怎么取表单传出来的参数?
  • javascript调用带有传出参数的函数
  • javascript 调用activeX中的方法,如何传入传出参数?
  • 为什么无法取得传出参数?SqlHelper---------------顶者有分
  • _declspec (dllexport) int UnPacketTCPData(LPCTSTR pStrValue, UPMSG &outUpMsg)传出参数的问题吗?
  • delphi调用vc编写的dll,传出参数问题
  • oracle中得传出参数怎么使用 ????
  • 请问如何用双击事件打开另一窗口,并传出参数
  • 关于dll,其是否可以传出多个参数??如何应用??

关键词

  • outparam
  • methods
  • keywords
  • argument
  • ref
  • passed
  • static void
  • are

得分解答快速导航

  • 帖主:gxhmsy
  • Macosx
  • happycharles

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo