C# ref and out

本文详细介绍了C#中ref和out参数的使用方法及其区别。ref参数要求调用方法和定义方法都要显式使用ref关键字,并且传递的参数必须先初始化。out参数则只需要在方法定义中使用out关键字,传递的参数不必预先初始化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 pass parameters with reference:

ref and out same:

both the method definition and the calling method must explicitly use the ref keyword.

both the method definition and the calling method must explicitly use the out keyword

static void Method(out int i, out string s1, out string s2)
    {
        i = 44;
        s1 = "I've been returned";
        s2 = null;
    }
    static void Main()
    {
        int value;
        string str1, str2;
        Method(out value, out str1, out str2);
        // value is now 44
        // str1 is now "I've been returned"
        // str2 is (still) null;
    }

Different:

An argument passed to a ref parameter must first be initialized. This differs from out, whose argument need not be explicitly initialized before being passed.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值