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

时间: 2024-08-01 05:38:31

C#使用ref和out传递数组的相关文章

ajax传递数组、form表单提交对象数组

在JSP页面开发中,我们常常会用到form表单做数据提交,由于以前一直只是使用form表单提交单个对象,只要表单文本域的name值和接收的对象的属性名一致,那么传值就没有什么问题.不过,在前几天的开发任务中,遇到了需要批量传递对象,也就是需要传递对象数组,在此做个总结.今天又遇到需要向后台传递数组,便一并写下来吧. 1.ajax传递普通数组 前台代码 var deleteNum= [];//定义要传递的数组 deleteNum.push("1"); deleteNum.push(&qu

C# ref和out传递参数总结

如有雷同,不胜荣幸,若转载,请注明 C#中ref和out传递参数总结,两个都可用来传递参数,ref使用时必须先进行初始化,out则不需要,只要在返回之前赋值即可,文字废话到此,下面直接上例子 ref例子 Class A { private string name = string.Empty; private int count = 0; ... GetName(ref name,ref count); Console.Write(“姓名:” + name + ",数量:" + cou

jquery ajax传递数组给php

写成:var data = {'item[]':item}; $.post(url,data,function(return_data) 写成item:item会导致数据缺失. 更多:http://www.cnblogs.com/ini_always/archive/2011/12/17/2291290.html jquery ajax传递数组给php,布布扣,bubuko.com

数组与指针的区别,以及在STL中传递数组/指针

数组和指针在作为实参传入T[] 或T*的形参时没有区别 void f(int pi[]) { cout << sizeof(pi) << endl; } int a[5] = { 1,2,3,4,5 }; f(a); 上述代码输出的是4(32位系统)或8(64位系统),总之不是sizeof(int) * 5(数组大小). 为什么明明形参是数组形式的int [],实际上和指针形式的int *无异呢?关键原因就在于,数组是不能作为左值的. 也就是说,你不能定义两个数组,比如int a[

前端Js传递数组至服务器端

相关学习资料 Linux黑客大曝光: 第8章 无线网络 无线网络安全攻防实战进阶 无线网络安全 黑客大曝光 第2版 http://zh.wikipedia.org/wiki/IEEE_802.11 http://www.hackingexposedwireless.com/doku.php http://blog.csdn.net/gueter/article/details/4812726 http://my.oschina.net/u/994235/blog/220586#OSC_h2_6

Jquery Ajax向服务端传递数组参数值

在使用MVC时,向服务器端发送POST请求时有时需要传递数组作为参数值 下面使用例子说明,首先看一下Action [HttpPost] public ActionResult Test(List<string> model) { return Json(null, JsonRequestBehavior.AllowGet); } 方式一,构造表单元素,然后调用serialize()方法得到构造参数字符串 @{ Layout = null; } <!DOCTYPE html> <

在ASP.NET MVC中以post方式传递数组参数的示例

最近在工作中用到了在ASP.NET MVC中以post方式传递数组参数的情况,记录下来,以供参考. 一.准备参数对象 在本例中,我会传递两个数组参数:一个字符串数组,一个自定义对象数组.这个自定义对象UserInfo定义如下: public class UserInfo { public int UserId { get; set; } public string UserName { get; set; } } 二.后台代码 后台Action代码如下: [HttpPost] public Ac

前端AJAX传递数组给Springmvc接收处理

前端传递数组后端(Spring)来接收并处理: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>测试页面</title> <script type="text/javascript" src="http://www.ostools.net/js/jquery/jquery-1.7.2.js">

Shell 向函数传递 数组

Shell 向函数传递 数组