C# 参数 , 返回值

有无参数方法

输出10次整数

// 无参数方法
public static void ProgramMothod(){
for (int i = 0;i<10;i++)
{
Console.WriteLine(1)
}
}


// 有参数方法
public static void ProgamMothod(int a){

for (int i = 0; i < 10; i++)
{
Console.WriteLine(a)
}
}

有无返回值

void 无返回值 代表结果无出口

// 有返回值,必须用 return (关键字) 把结果传出去
public static int ProgramMothod(int a){

Console.WriteLine(a);
return a;
}
/ 调用
int retrunNumber = ProgramMothod(300);



//无返回值 void 无返回值,代表方法结果无出口
public stat void PorgeamMothod1(int a ){
Console.WriteLine(a-1);
}
/ 调用
ProgramMothod1(100);

参数传递

ref 

// 参数传递形式

public static void PorgramMothod(int a,int b){

// 交换a和b的值,如果交换之后 不影响外部 tempA 和 tempB的值    引用参数传递
int c;
c=a;
a=b;
b=c;
}
// 调用
int tempA = 100;
int tempB = 200;
ProgramMothod(tempA,tempB);

Console.WriteLine(tempA);
Console.WriteLine(tempB);




//  引用参数 如果交换之后 影响外部tempA和tempB的值  引用参数传递
        public static void ProgramMothod1(ref int a,ref int b)
        {

            //交换a和b的值  如果交换之后 不影响外部tempA和tempB的值  就是值参数传递   
            int c;
            c = a;
            a = b;
            b = c;
        }
///  调用方法
            int tempC = 300;
            int tempD = 400;
            ProgramMothod1(ref tempC,ref tempD);
            Console.WriteLine(tempC);
            Console.WriteLine(tempD);







// 输出参数
//1.是对于方法返回值的补充。return 可用于函数中返回一个值 输出参数可以返回多个值
//2关键字out: out输出参数在方法中 必须被使用,且和return保持一致
//3其他方法引用参数相似
 public static void ProgramMothod3(out int a, out int b,out int c)
        {
                b = 20;
                c = 30;
                a=1000;   
        }
// 调用方法
            int tempB1;
            int tempC2;
            int tempA1;
            ProgramMothod3(out tempA1,out tempB1,out tempC2);
            Console.WriteLine(tempA1);
            Console.WriteLine(tempB1);
            Console.WriteLine(tempC2);

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值