C# 方法参数 值类型 对象类型 ref

对象型参数

第一个方法: ref参数    

Dictionary<int, string> dic = new Dictionary<int, string>();
    dic.Add(1, "第一个");

    private void changeRefDic(ref Dictionary<int, string> dic)
    {
        dic.Add(3, "第三个");
    }

调用之后,dic的 key.count = 2

第二个方法: 普通对象型的参数, 但是没有再New
    Dictionary<int, string> dic = new Dictionary<int, string>();
    dic.Add(1, "第一个");

    private void changeDic(Dictionary<int, string> dic)
    {       
        dic.Add(2, "第二个");
    }

调用之后,dic的 key.count = 2

第三个方法 : 普通的对象型参数,在方法体里面有New,重新初始化过.
    Dictionary<int, string> dic = new Dictionary<int, string>();
    dic.Add(1, "第一个");
    private void changeNewDic(Dictionary<int, string> dic)
    {
        dic = new Dictionary<int, string>();
        dic.Add(2, "第二个");
    }

调用之后,dic的key.count = 1


值类型参数

方法一: Ref值类型.
    string str = "123";
    private void changeRefStr(ref string str)
    {
        str = "123456";
    }

调用此方法后: str的值为 " 123456"

方法二: 普通参数.

 string str = "123";

    private void changeStr(ref string str)

    {
        str = "123456";
    }

调用此方法后: str的值为 "123";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值