A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 10642491 黑马帝   /  2011-10-27 08:08  /  3206 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

原来C#中还有这两个关键字,不过一直未用过它们,也不知道它们在项目开发中是否会用到。

评分

参与人数 1技术分 +1 收起 理由
官方工作人员 + 1 赞一个!

查看全部评分

8 个回复

倒序浏览
有,比如分页的时候。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int i = 3;
  12.             Console.WriteLine("i之前{0}", i);
  13.             PrintOut(out i);
  14.             Console.WriteLine("i之后{0}", i);

  15.             int j = 4;
  16.             Console.WriteLine("i之前{0}", j);
  17.             PrintRef(ref j);
  18.             Console.WriteLine("i之后{0}", j);

  19.             int z;
  20.             PrintOut(out z);
  21.             Console.WriteLine("z之后{0}", z);

  22.         }

  23.         static void PrintOut(out int i)
  24.         {
  25.             i = 1;
  26.         }

  27.         static void PrintRef(ref int i)
  28.         {
  29.             i = 2;
  30.         }
  31.     }
  32.    

  33. }
复制代码

从例子中看出:
out 可以给初始值可以不给,但ref一定要给初始值
还有就是重载方法中,ref和out只能出现一个,看下面的方法只能出现一个
  1. static void PrintOut(out int i)
  2.         {
  3.             i = 1;
  4.         }

  5.         static void PrintOut(ref int i)
  6.         {
  7.             i = 1;
  8.         }
复制代码
回复 使用道具 举报
ref和out都是通过引用来传递参数,不同的是ref要求变量必须在传递之前进行初始化,楼上已经给出很详细的代码。
回复 使用道具 举报
out是传出参数的. ref即可以传入参数也可以传出参数.   如果你一个方法 需要计算整数数组的几个数字的和.还想找数组中的最大值和最小值.  可以定义这个方法是用返回值接收数据的和. 用out传出参数把数组中的最大值和最小值传出.   实际工作中ref 一般也当作out功能用.  不知道是否明白了.等我不忙时写个代码给大家看一下.  
回复 使用道具 举报
黄朝辉 发表于 2011-10-27 09:37
有,比如分页的时候。
从例子中看出:
out 可以给初始值可以不给,但ref一定要给初始值

讲的很详细,谢谢呵呵
回复 使用道具 举报
一句话:
ref 有进有出
out 有出未必有进

评分

参与人数 1技术分 +1 收起 理由
官方工作人员 + 1 赞一个!

查看全部评分

回复 使用道具 举报
static void Main(string[] args)
        {
            int number;//要定义一个变量要接收Test()方法中的out 参数传出来的值
            int result = Test(out number);//调用方法时接收参数的变量前也加out关键字
            //输出结果为number=11 result=200;
            Console.WriteLine("number={0},result={1}", number, result);
            Console.ReadKey();
        }
        //定义一个测试方法参数为out传出参数
        static int Test(out int a)
        {
            //参数中的变量a必须在方法中赋值;
            a = 10;
            a = a + 1;
            return 200;
        }
回复 使用道具 举报
static void Main(string[] args)
        {
            int a=0; //必须赋值
            int b=1;
            
            Jiaohuan(ref b, ref a);
            Console.ReadKey();

        }

        //定义一个方法来交换两个变量的值
        static void Jiaohuan(ref int a,ref int b)
        {
            a = 4;
            b = 5;
            Console.WriteLine(a);
            Console.WriteLine(b);

        }
回复 使用道具 举报
10642491 黑马帝 2011-10-28 11:40:28
9#
小张老师 发表于 2011-10-28 11:21
static void Main(string[] args)
        {
            int a=0; //必须赋值

谢谢老师,讲得很好呵呵。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马