博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# out 与 ref 关键字 就是传递引用
阅读量:2026 次
发布时间:2019-04-28

本文共 974 字,大约阅读时间需要 3 分钟。

using System;namespace ConsoleApplication13{    class Program    {        static void outTest(out int x, out int y)        {//离开这个函数前,必须对x和y赋值,否则会报错。          //y = x;          //上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行             x = 1;            y = 2;        }        static void refTest(ref int x, ref int y)        {            x += 1;            y += x;        }        public static void Main()        {            //out test            int a, b;            //out使用前,变量可以不赋值            outTest(out a, out b);            Console.WriteLine("a={0};b={1}", a, b);            //无进有出            int c = 11, d = 22;            outTest(out c, out d);            Console.WriteLine("c={0};d={1}", c, d);            //ref test            //int m, n;            //refTest(ref m, ref n);             //上面这行会出错,ref使用前,变量必须赋值            //有进有出            int o = 11, p = 22;            refTest(ref o, ref p);            Console.WriteLine("o={0};p={1}", o, p);        }    }}

转载地址:http://ctdaf.baihongyu.com/

你可能感兴趣的文章
05.软件项目管理与敏捷方法——范围管理笔记
查看>>
00.敏捷回顾——引言笔记
查看>>
python学习手册笔记——30.类的设计
查看>>
Big Analytice with Cassandra
查看>>
spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序)
查看>>
leetcode 之 Single Number II
查看>>
关于AOP无法切入同类调用方法的问题
查看>>
[LeetCode] 268. Missing Number ☆(丢失的数字)
查看>>
http1.0 1.1 2.0区别
查看>>
spring bean生命周期
查看>>
从线程模型的角度看Netty的高性能
查看>>
[LeetCode] 20. Valid Parentheses ☆(括号匹配问题)
查看>>
Mysql可重复读、避免幻读原理
查看>>
父类上的注解能被子类继承吗
查看>>
[LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
查看>>
二分变种
查看>>
Zookeeper集群为什么要是单数
查看>>
TCP窗口滑动以及拥塞控制
查看>>
[LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)
查看>>
大批量数据处理方法
查看>>