C#关于ref的用法(多个实参值的传递)
按照C#默认的按值调用参数的传递机制,不能刻编写出一个方法来实现两个int类型的值交换,因为一个方法只能对应一个返回值,如何实现将两个交换的值传递回去,这里我将用到的是ref修饰符。
使用ref的单值传递(没有返回值,但是直接将实参的值做了修改,如果是两个int型做值交换,ref也将直接对其实参进行修改为值交换后的值)
ps:这里说的有些不对,但是大体思路是这个样子,看例子自己领悟吧。就是在方法中直接对原本传进来的值进行修改。不需要return
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace cxx
{
class RefTest
{
public void sqr( ref int i) //注意ref实在所有参数类型的最前面
{
i = i * i;
} } class refDemo
{
static void Main()
{
RefTest ob = new RefTest();
int a = ; Console.WriteLine("a before call:" + a); ob.sqr(ref a); //还是在最前面 Console.WriteLine("a after call:" +a);
Console.WriteLine(@"my name is shoneworn
my blog: www.cnblogs.com/shoneworn
welcome to my blog !");
//对自己的博客做一下推广,同时也复习一下“@”的用法 Console.ReadKey(); }
}
}
常规单值传递(不适用ref):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace cxx
{
class RefTest
{
public int sqr( int i)
{
return i = i * i; //注意方法类型为int型,需要用到return来返回值
} } class refDemo
{
static void Main()
{
RefTest ob = new RefTest();
int a = ; Console.WriteLine("a before call:" + a); int b = ob.sqr( a); //用b来接收值 Console.WriteLine("a after call:" + b);
Console.WriteLine(@"my name is shoneworn
my blog: www.cnblogs.com/shoneworn
welcome to my blog !"); //对自己的博客做一下推广,同时也复习一下“@”的用法 Console.ReadKey(); }
}
}
C#关于ref的用法(多个实参值的传递)的更多相关文章
- React之ref详细用法
在react典型的数据流中,props传递是父子组件交互的唯一方式:通过传递一个新的props值来使子组件重新re-render,从而达到父子组件通信.当然,就像react官网所描述的一样,在reac ...
- vue里ref ($refs)用法
ref 有三种用法: 1.ref 加在普通的元素上,用this.ref.name 获取到的是dom元素 2.ref 加在子组件上,用this.ref.name 获取到的是组件实例,可以使用组件的所有方 ...
- oracle中REF Cursor用法
from:http://www.111cn.net/database/Oracle/42873.htm 1,什么是 REF游标 ? 动态关联结果集的临时对象.即在运行的时候动态决定执行查询. 2,RE ...
- 转载:老生常谈C++中实参形参的传递问题
以下文章转载自:http://www.jb51.net/article/108390.htm 函数中参数的传递 这里说的传递当然是指 实参是如何传递给形参的啦 还挺复杂的~~~~~~~~⊙﹏⊙b汗,这 ...
- ref和out的区别,值类型和引用类型的使用
今天刚刚明白ref和out的区别,只限于个人理解如有不同请赐教,谢谢 首先我感觉ref和out是针对于值类型来说,以前一直认为是针对于引用类型看下面的一段代码 1.首先结果 i=0:ints[0]=0 ...
- React ref的用法
React的ref有3种用法: 1. 字符串(已废弃)2. 回调函数3. React.createRef() (React16.3提供) 1. 字符串 最早的ref用法. 1.dom节点上使用,通过t ...
- React Native中ref的用法(通过组件的ref属性,来获取真实的组件)
ref是什么? ref是组件的特殊属性,组件被渲染后,指向组件的一个引用.可以通过组件的ref属性,来获取真实的组件.因为,组件并不是真正的DOM节点,而是存在于内存中的一种数据结构,称为虚拟的DOM ...
- ref的用法
ref可以修改变量作为参数调用的值,但必须要初始化,不能在调用的函数中初始化 static void Main(string[] args) { ; Console.WriteLine("m ...
- Vue.js中ref ($refs)用法举例总结
原文地址:http://www.cnblogs.com/xueweijie/p/6907676.html <div id="app"> <input type=& ...
随机推荐
- js获取名字为XX的标签
$("input[name='XX']"); <input name="address_select" type="radio" va ...
- TransactionScrope 2
继上一篇文章TransactionScrope 在做相应的变动时,发现可以重现ORA-14450错误,如: List<Thread> ls = new List<Thread> ...
- Unity-资源
模型.材质.动画 Unity的默认系统单位为"米" 三维软件 三维软件内部米制尺寸/m 默认设置导入Unity中的尺寸/m 与Unity单位的比例关系 Maya 1 100 1:1 ...
- Python codes
Vertification of an assignment from Stochastic Processing. Using Brute Force and "itertools&quo ...
- [poj2449]Remmarguts' Date(spfa+A*)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Remmarguts' Date Time Limit: 4000MS Mem ...
- linux cat命令的<<EOF
初初开始学习linux的命令,只对linux一些简单命令有一些了解! 首先我看到网上有一些创建一个文件采用的命令是(mkdir创建文件夹):cat > test1.txt <<EOF ...
- bootstrap的datetimepicker控件只选择年月的配置
<script src="{% static "jquery/jquery-1.11.3.min.js" %}"></script> & ...
- 连接oracle数据库出现:ORA-12505,TNS:listener does not currently know of SID given in connect descriptor
Java使用 jdbc:oracle:thin:@11.1.0.14:1521:orcl 连接oracle数据库出现: ORA-12505,TNS:listener does not currentl ...
- Sumsets(POJ 2229 DP)
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 15293 Accepted: 6073 Descrip ...
- Win7网络检测 WindowsAPICodePack
原文:http://www.cnblogs.com/yincheng01/archive/2010/05/30/2213234.html 在Windows7操作系统下,支持的网络类型越来越复杂,微软提 ...