C#使用ref和out传递数组
一、使用ref参数传递数组
数组类型的ref参数必须由调用方明确赋值。因此,接受方不需要明确赋值。接受方数组类型的ref参数能够修改调用方数组类型的结果。可以将接受方的数组赋以null值,或将其初始化为另一个数组。请阅读引用型参数。
示例:
在调用方法(Main方法)中初始化数组array,并使用ref参数将其传递给theArray方法。在theArray方法中更新某些数组元素,然后将数组元素返回调用方并显示出来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void theArray(ref int[] arr) // 接受方不需要明确赋值
{
if (arr == null) // 根据需要创建一个新的数组(不是必须的)
{
arr = new int[10]; // 将数组赋以null值,或将其初始化为另一个数组
}
arr[0] = 11;
arr[4] = 55;
}
static void Main(string[] args)
{
// C#使用ref参数传递数组-www.baike369.com
int[] array = { 1, 2, 3, 4, 5 };
theArray(ref array); // 使用ref传递数组,调用方明确赋值
Console.Write("数组元素为:");
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组元素为:11 2 3 4 55
二、使用out参数传递数组
被调用方在使用数组类型的out参数时必须为其赋值。请阅读输出参数。
示例:
在调用方方法(Main方法)中声明数组array,并在theArray方法中初始化此数组。然后将数组元素返回到调用方并显示出来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void theArray(out int[] arr)
{
arr = new int[]{0,2,4,6,8}; // 必须赋值
}
static void Main(string[] args)
{
int[] array;
theArray(out array); // 传递数组给调用方
Console.Write("数组元素为:"); // 显示数组元素
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组元素为:0 2 4 6 8
C#使用ref和out传递数组的更多相关文章
- 使用 ref 和 out 传递数组注意事项
1.与所有的 out参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由被调用方为其赋值 示例 :在此例中,在调用方(Main 方法)中声明数组 theArray,并在 FillArra ...
- 前端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( ...
随机推荐
- ajax禁止浏览器缓存
把cache 设置为false ,把 ifModified 设置为true //工作计划function workprogram(date_time){ $.ajax({ asyn ...
- UGUI [TextArea]
- 编译opencv python接口
首先,安装依赖1 sudo apt-get install build-essential 2 sudo apt-get install cmake git libgtk2.0-dev pkg-con ...
- Python 元组 (tuple)
作者博文地址:https://www.cnblogs.com/liu-shuai/ Python的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套.不同之处在于元组的元素不能修改.元组使用小括号 ...
- 前端:移动端和PC端的区别
在阿里的几次面试中,总是被问到移动端和PC端有什么区别,当时回答的时候主要是回答了在兼容性.网速.适配.页面布局等方面的不同,但是还是很不系统,所以这里做一个总结. 第一: PC考虑的是浏览器的兼容性 ...
- 关于数学问题的urls
一个知乎账号, 分析了很多的数学问题: https://www.zhihu.com/people/matongxue/activities 关于三阶样条的解析: https://blog.csdn.n ...
- HDU——Cover——————【技巧】
Cover Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- gdb调试汇总
1. 启动GDB开始调试: (1)gdb program ///最常用的用gdb启动程序,开始调试的方式(2)gdb program core ///用gdb查看core dump文件,跟踪程序cor ...
- C# params 动态参数
public delegate void Action(params object[] args); 再简单的东西都要强迫自己记录了,前段时间硬盘坏了,资料全没了,也没有备份,太痛苦了,那么多资料全没 ...
- 微信公众号自动回复_Java
先声明一下,这是一个maven工程pom文件需要的依赖: <dependency> <groupId>dom4j</groupId> <artifactId& ...