值传递 引用传递 输出传递
static class Program
{
//值传递调用
int x = 20;
int y = 30; // 定义
TestMethod10(x, y); // 传值
Console.WriteLine("x y调换后{0},{1}", x, y);
int q = 200;
int w = 300;
Testmethod11(ref q,ref w);
console.WriteLine("x,y调换后{0},{1},q,w")
int c ;
bool isTrue = TestMethods12(out c)
Console.WriteLine("retrun结果:{0},输出参数结果:{1}",isTrue,c)
int d;
int e;
bool isXiaoHu = TestMothod13(10,20,out d, out e)
Console.WriteLine("retrun结果{0},输出参数结果d:{1},输出参数结果e:{2}",isXiangHu,d,e);
}
1.值传递
特点 1.参数传递的默认方式
2.当调用一个方法时,会为每个参数创建一个新的存储位置
3.当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全
public static viod TestMethod10(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
Console.WriteLine("a和b调换后的值{0},{1}", a, b);
}
2.引用传递
1.引用参数是一个对变量的内存位置的引用 不会创建一个新的存储位置
2. 参数关键字 ref
public static void TestMethod11(ref int a, ref int b)
{
int temp;
temp = a;
a = b;
b = temp;
Console.WriteLine("a和b调换后的值{0},{1}", a, b);
}
3.输出参数
1.是对于返回方法的补充, return 语句可用于 只从函数中返回一个值 输出参数可以返回多个值
2.关键字 out
3.其他方面 与引用参数类似
public static bool TestMethod12(out int a)
{
a = 2000;
return true;
}
2
public static bool TestMethod13(int x,int y,out int a,out int b)
{
if(x==y)
{
a =x;
b=y;
return true
}
else
{
a = y;
b = x;
return false;
}
}
2467

被折叠的 条评论
为什么被折叠?



