我需要不断重复的为一个实体赋值,但是实体中部分元素的值是需要改变的这是我现在的做法,有没有更简便的方法?
1 List entityList = new List(); 2 MyEntity entity = new MyEntity (); 3 entity .Name = name; 4 entity .Age= 25; 5 entity .Address= "地址1111"; 6 entityList .Add(entity ); 7 8 entity = new MyEntity (); 9 entity .Name = name; 10 entity .Age= 55; 11 entity .Address= "地址3333"; 12 entityList .Add(entity ); 13 14 entity .Name = name; 15 entity .Age= 23; 16 entity .Address= "地址4343"; 17 entityList .Add(entity );
string name=string.Empty; List<MyEntity> list = new List<MyEntity>() { new MyEntity(){Name=name,Age=25,Address="地址1111"}, new MyEntity(){Name=name,Age=55,Address="地址3333"}, new MyEntity(){Name=name,Age=35,Address="地址4343"}, };
List entityList = new List(); updateList(name,25,"地址1111"); ... public List updateList(string name,int age, string addr){ MyEntity entity = new MyEntity (); entity .Name = name; entity .Age= age; entity .Address= addr; entityList .Add(entity ); }
这样好点
MyEntity entity = new MyEntity { Name = name, Age= 25, Address= "地址111" } entityList .Add(entity );
你的代码是有bug的.
哪里?
我把一楼和二楼的结合了一下