//引用传递参数 即函数处理变量与函数调用中使用的变量相同,而不仅仅是指相同的变量。因此对这个变量进行任何改变都会影响用作参数的变量值。为此需要定义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)); } } }
//函数的参数也可以为参数数组,用于不知道将传入多少个参数时使用 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)); } } }
{
pp[0] = 1;
pp[1] = 2;
}你这里的赋值,说明你希望是int数组,但你的可变长度为object[],你无论如何也实现不了ref的,因为已经是装箱后的了。还有一个规则,可变长度的不可以修饰为ref.
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";
.... 对参数赋值。
}请高手们赐教。
http://topic.csdn.net/u/20101222/15/a4c712a0-01d1-4d25-87c2-48c65d9368c7.html
--------------------------------------你不用 ref 也会得到赋值后的结果你先弄明白我在#1的回复吧
不用 ref,是改变不了值的。测试都不用测试。
不用 ref, 只是值传递,等于“复制”。
ref,是变量地址引用。
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());用起来就和不定参数差不多了,缺点很明显,如果参数多的话,要写很多很多函数.....
public class MyParam {
public int Value { get; set; }
} public int AAAA(MyParam[] pp) {
pp[0].Value = 1;
pp[1].Value = 2;
return pp.Length;
}
既然是有点业务设计技术含量的东西,干吗就认真准了编程语法的特定噱头呢?用平易近人的普通class封装你的业务。
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)); }
}
}
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)); }
}
}