params 可变参数,又需要这些参数是 ref 引用型的。
public int AAAA (params object[] pp)    //   ref 怎么加?
{
    pp[0] = 1;
    pp[1] = 2;
    ....    对参数赋值。
}请高手们赐教。

解决方案 »

  1.   

    你现在能区分方法参数 加 ref 与 不加 ref 传递引用类型的区别么?
      

  2.   

    简单的说,按照你现在的描述,你根本不需要 ref 
      

  3.   

    public int AAAA (params object[] pp) // ref 怎么加?
    {
      pp[0] = 1;
      pp[1] = 2;
    }你这里的赋值,说明你希望是int数组,但你的可变长度为object[],你无论如何也实现不了ref的,因为已经是装箱后的了。还有一个规则,可变长度的不可以修饰为ref.
      

  4.   

    题目稍微修改下:
    params 可变参数,又需要这些参数是 ref 引用型的。public void AAAA1()
    {
        int a = 0;
        string b = "";    AAAA2(ref a, ref b);    // 这里,a, b 应该得到赋值后的结果。
    }public void AAAA2 (params object[] pp) // ref 怎么加?
    {
      pp[0] = 1;
      pp[1] = "aaaa";
      ....  对参数赋值。
    }请高手们赐教。
      

  5.   

    msdn上写了不可以这么做。你太2了。
      

  6.   

    求救希望大侠能给我解决解决问题谢谢!
    http://topic.csdn.net/u/20101222/15/a4c712a0-01d1-4d25-87c2-48c65d9368c7.html
      

  7.   

    AAAA2(ref a, ref b);  // 这里,a, b 应该得到赋值后的结果。
    --------------------------------------你不用 ref 也会得到赋值后的结果你先弄明白我在#1的回复吧
      

  8.   

    非也,不是钻牛角尖。是项目中,由于业务问题,有一个只是参数个数不同,内容大同小异的方法,总共 90 多个。是用于解析 tcp/ip 数据包用的。所以,我想合并成一个而已。
      

  9.   

    To:  Sandy945
    不用 ref,是改变不了值的。测试都不用测试。
    不用 ref, 只是值传递,等于“复制”。
    ref,是变量地址引用。
      

  10.   


     public int AAAA1(ref int p1)
        {
            p1 = 1;        return 1;
        }    public int AAAA1(ref int p1, ref int p2)
        {
            p1 = 1;
            p2 = 2;        return 1;
        }    public int AAAA1(ref int p1, ref int p2, ref int p3)
        {
            p1 = 1;
            p2 = 2;
            p3 = 3;
            return 1;
        }这个是重载函数,使用的时候
     int p1=0,p2=0,p3=0;
    //  AAAA1(ref p1);
    //  AAAA1(ref p1,ref p2);
      AAAA1(ref p1,ref p2,ref p3);  Response.Write(p1.ToString() + p2.ToString() + p3.ToString());用起来就和不定参数差不多了,缺点很明显,如果参数多的话,要写很多很多函数.....
      

  11.   


        public class MyParam {
            public int Value { get; set; }
            }    public int AAAA(MyParam[] pp) {
            pp[0].Value = 1;
            pp[1].Value = 2;
            return pp.Length;
            }
      

  12.   


    既然是有点业务设计技术含量的东西,干吗就认真准了编程语法的特定噱头呢?用平易近人的普通class封装你的业务。
      

  13.   

    我更偏重于用数组,传址。最好大小也传。看看底层代码,Microsoft的技术基本都是用这种方式。
      

  14.   

    http://msdn.microsoft.com/zh-cn/library/w5zay9db(v=VS.80).aspx在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。你的方法中去掉params 就可以了,反正都已经是object数组了,长度可变的
      

  15.   

    你看看能不能用ArrayList代替可变参数数组,这样就可以ref了。。
      

  16.   

    //引用传递参数  即函数处理变量与函数调用中使用的变量相同,而不仅仅是指相同的变量。因此对这个变量进行任何改变都会影响用作参数的变量值。为此需要定义ref关键字指定参数。 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace wintest1
    {
        public partial class Form1 : Form
        {        static int showDouble(ref int val)
            {
                val = val * 2;
                return val;
            }        public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                int val=5;
                MessageBox.Show(string.Format("未使用引用的val值{0}", val));
                showDouble(ref val);
                MessageBox.Show(string.Format("使用引用后的val值{0}", val));
                        }
        }
    }------------------------------------------------------------------------------------------//函数输出参数  out 为关键字 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace wintest1
    {
        public partial class Form1 : Form
        {
            static int MaxValue(int[] intArray, out int maxIndex)
            {
                int maxVal = intArray[0];
                maxIndex = 0;
                for (int i = 1; i < intArray.Length; i++)
                {
                    if (intArray[i] > maxVal)
                    {
                        maxVal = intArray[i];
                        maxIndex = i;
                    }
                }
                return maxVal;
            }        public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                int[] myArray={1,2,5,6,3,4,9,8,7};
                int maxIndex;
                MessageBox.Show(string.Format("最大值{0}", MaxValue(myArray, out maxIndex)));
                MessageBox.Show(string.Format("最大值为第{0}个数值", maxIndex + 1));        }
        }
    }
      

  17.   

    //函数的参数也可以为参数数组,用于不知道将传入多少个参数时使用 params 定义为参数数组,可以接受任意多个和没有任何数据的参数   using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace wintest1
    {
        public partial class Form1 : Form
        {
            static int sumVals(params int[] vals)
            {
                int sum = 0;
                foreach (int val in vals)
                {
                    sum = sum + val;
                }
                return sum;
            }        public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                int sum = sumVals(1, 5, 2, 9, 8);
                MessageBox.Show(string.Format("参数数组相加后的值为{0}", sum));        }
        }
    }
      

  18.   

    这个不需要ref,直接传数组过去是同样的效果——传的是指针。
      

  19.   

    的确是,不管你数组里面的元素是什么类型,你现在已经放在object[]里面了,所以就没必要加ref了,如果你先让object[]的长度可变要不就加 params,要不就用ArrayList。