using System;
using System.Collections.Generic;
using System.Text;namespace test
{
class Program
{
static void Main(string[] args)
{
Person person1 = new Person();
SetPerson( person1);
} private static void SetPerson(Person person, string ss)
{
person = new Person();
person.Id1 = Guid.NewGuid().ToString();
person.Name1 = "张三";
person.Age1 = 22;
}
} public class Person
{
string Id = string.Empty; public string Id1
{
get { return Id; }
set { Id = value; }
}
string Name = string.Empty; public string Name1
{
get { return Name; }
set { Name = value; }
}
int Age = 0; public int Age1
{
get { return Age; }
set { Age = value; }
}
}
}
在主函数MAIN中调用SetPerson方法,个person对象传递了过去,在SetPerson中队person重新实例化了一下,这次实例化person发生了什么变化,导致引用传参赋值失败?希望大家能从原理上给我解答。
using System.Collections.Generic;
using System.Text;namespace test
{
class Program
{
static void Main(string[] args)
{
Person person1 = new Person();
SetPerson( person1);
} private static void SetPerson(Person person, string ss)
{
person = new Person();
person.Id1 = Guid.NewGuid().ToString();
person.Name1 = "张三";
person.Age1 = 22;
}
} public class Person
{
string Id = string.Empty; public string Id1
{
get { return Id; }
set { Id = value; }
}
string Name = string.Empty; public string Name1
{
get { return Name; }
set { Name = value; }
}
int Age = 0; public int Age1
{
get { return Age; }
set { Age = value; }
}
}
}
在主函数MAIN中调用SetPerson方法,个person对象传递了过去,在SetPerson中队person重新实例化了一下,这次实例化person发生了什么变化,导致引用传参赋值失败?希望大家能从原理上给我解答。
{
person = new Person(); //将变量 person 指向一个新的Person实例,该实例在 heap 中分配内存
person.Id1 = Guid.NewGuid().ToString();
person.Name1 = "张三";
person.Age1 = 22;
}
下面的地址讲了一些关于内存管理的知识,你可以看看。
http://www.cnblogs.com/jiejie_peng/archive/2011/04/24/2026272.html