马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

关于ref和out怎么用,有什么含义

[复制链接]
查看3377 | 回复2 | 2013-11-18 18:50:48 | 显示全部楼层 |阅读模式
//ref
class Program
    {
        public static void Change(ref int x, ref int y)
        {
            int t = x;
            x = y;
            y = t;

        }
        public static void Jia(ref int x)
        {
            x++;

        }
        static void Main(string[] args)
        {
            int a = 15, b = 17;

            Change(ref a,ref b);
            Jia(ref a);
            Jia(ref b);

            Console.WriteLine("当前a的值为{0}", a);
            Console.WriteLine("当前b的值为{0}", b);
           //out
class Program    {        public static void Change(out int x, ref int y)        {            int t = x;            x = y;            y = t;              }        public static void Jia(out int x)        {            x++;                  }        static void Main(string[] args)        {            int a = 15, b = 17;                Change(ref a,ref b);            Jia(ref a);            Jia(ref b);                     Console.WriteLine("当前a的值为{0}", a);            Console.WriteLine("当前b的值为{0}", b);

ibcadmin | 2013-11-18 22:08:48 | 显示全部楼层
1、使用ref型参数时,传入的参数必须先被初始化。对out而言,必须在方法中对其完成初始化。

2、使用ref和out时,在方法的参数和执行方法时,都要加Ref或Out关键字。以满足匹配。

3、out适合用在需要retrun多个返回值的地方,而ref则用在需要被调用的方法修改调用者的引用的时候。
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
Baby'sBreath | 2013-11-18 23:02:54 | 显示全部楼层
不是很多,但还是非常感谢
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则