对象型参数
第一个方法: 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, "第二个");
}
方法一: 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";
第一个方法: 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";