使用 ref 和 out 传递数组注意事项
1、与所有的 out参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由被调用方为其赋值
class TestOut
{
static void FillArray(out int[] arr)
{
//初始化数组:
arr = new int[] { , , , , };
} static void Main()
{
int[] theArray; // 可以不用初始化数组 //使用out传递数组
FillArray(out theArray); System.Console.WriteLine("数组结果:");
for (int i = ; i < theArray.Length; i++)
{
System.Console.Write(theArray[i] + " ");
}
}
}
1 2 3 4 5
2、与所有的 ref 参数一样,数组类型的 ref 参数必须由调用方明确赋值。因此不需要由接受方明确赋值。可以将数组类型的 ref 参数更改为调用的结果。例如,可以为数组赋以 null 值,或将其初始化为另一个数组。
示例在此例中,在调用方(Main 方法)中初始化数组 theArray,并通过使用 ref 参数将其传递给 FillArray 方法。在 FillArray 方法中更新某些数组元素。然后将数组元素返回调用方并显示。
class TestRef
{
static void FillArray(ref int[] arr)
{ if (arr == null)
{
arr = new int[];
} arr[] = ;
arr[] = ;
} static void Main()
{
// 必须初始化数组
int[] theArray = { , , , , }; // 使用ref传递数组
FillArray(ref theArray); System.Console.WriteLine("数组结果:");
for (int i = ; i < theArray.Length; i++)
{
System.Console.Write(theArray[i] + " ");
}
}
}
输出
数组结果:
1111 2 3 4 5555
使用 ref 和 out 传递数组注意事项的更多相关文章
- C#使用ref和out传递数组
C#使用ref和out传递数组 一.使用ref参数传递数组 数组类型的ref参数必须由调用方明确赋值.因此,接受方不需要明确赋值.接受方数组类型的ref参数能够修改调用方数组类型的结果.可以将接受方的 ...
- 前端AJAX传递数组给Springmvc接收处理
前端传递数组后端(Spring)来接收并处理: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- struts2 传递数组、List、Map
struts2 传递数组.List.Map jsp文件 数组: <s:textfield name="ages" value="a1">&l ...
- 在ASP.NET MVC中以post方式传递数组参数的示例
最近在工作中用到了在ASP.NET MVC中以post方式传递数组参数的情况,记录下来,以供参考. 一.准备参数对象 在本例中,我会传递两个数组参数:一个字符串数组,一个自定义对象数组.这个自定义对象 ...
- jquery ajax post 传递数组 ,多checkbox 取值
jquery ajax post 传递数组 ,多checkbox 取值 http://w8700569.iteye.com/blog/1954396 使用$.each(function(){});可以 ...
- Jquery post 传递数组给asp.net mvc方法
以批量删除数据为例 做批量删除会需要传递要删除的数据ID数组 function RemoveLog(){ var postModel=[]; //遍历复选框获取要删除的数据ID 存放到数组中 $( ...
- c语言函数传递数组
1.传递数组,打印不出来 #include <stdio.h> void solve() { printf(]); } int main() { int i; ;i<n;i++) { ...
- jquery ajax传递数组给php
写成:var data = {'item[]':item}; $.post(url,data,function(return_data) 写成item:item会导致数据缺失. 更多:http://w ...
- C++ 数组作为函数参数时,传递数组大小的方法
废话不多说,先上错误示范: void fun(int arr[arr_num]) { // ... } int main() { // ... int *arr = new int[10]; fun( ...
随机推荐
- guava缓存第一篇
guava缓存主要有2个接口,Cache和LoadingCache. Cache,全类名是com.google.common.cache.Cache,支持泛型.Cache<K, V>. L ...
- input框与img在同一行对齐
将input和img放同一行,img标签总是比input高出一个头,难看.后来在网站搜到最多的就是给img添加一个align="absmiddle"属性,这个方法似乎的确可行,但是 ...
- SpringBoot项目中,cookie的设置与销毁
cookie的设置与销毁 1.设置cookie /** * 设置一个cookie * @param response HttpServletResponse * @param name cookie的 ...
- java agent问题
Error occurred during initialization of VMagent library failed to init: instrumentobjc[36987]: Class ...
- python3.x使用cxfreeze将.p打包成.exe
之前写了一个使用ffplay批量查看格式为h264的图片,每次抽帧后都要打开pycharm编译器来运行程序,然后才能正常查看图片,或者在其他没有安装python环境的电脑中运行,很不方便.为此,在网上 ...
- 搭建私有git仓库gogs
安装 gogs 下载 gogs download 安装 解压压缩包. 使用命令 cd 进入到刚刚创建的目录. 执行命令 ./gogs web,然后,就没有然后了. #后台运行 $ nohup ./go ...
- [BZOJ4456][ZJOI2016]旅行者:分治+最短路
分析 类似于点分治的思想,只统计经过分割线的最短路,然后把地图一分为二. 代码 #include <bits/stdc++.h> #define rin(i,a,b) for(regist ...
- wannafly 练习赛11 E 求最值(平面最近点对)
链接:https://www.nowcoder.com/acm/contest/59/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...
- Linux shell - cut命令用法(转载)
cut [-bn] [file] 或 cut [-c] [file] 或 cut [-df] [file] 使用说明 cut 命令从文件的每一行剪切字节.字符和字段并将这些字节.字符和字段写至标 ...
- modern php笔记---php (性状)
modern php笔记---php (性状) 一.总结 一句话总结: trait是和继承一个层次的东西 一个类use MyTrait;后,trait中的方法覆盖父类方法,当前类中的方法覆盖trait ...