如何ObservableCollection里的数组值保持不变?

小可丁儿 2013-10-20 09:53:30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Collections.ObjectModel;

namespace SilverlightApplication5
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<Person> personList = new ObservableCollection<Person>();
Person pr = new Person();
for (int i = 0; i < 10; i++)
{
pr.PoemName = i.ToString();
personList.Add(pr);
}
int c = personList.Count;
}
public class Person
{
public string PoemName
{
get;
set;
}
}
}
}

代码如上,我希望personList 里的Person的属性Poemname,保存每个i的值,但是,因为Person 实在for循环外面实例化的,导致personList 数组里的PoemName属性,最后都变成了9,请问,该如何修改改行代码?
另外,这个只是个简单的演示代码,请不要到说,吧Person在for循环里实力化,因为,我的实际代码,是读一段文章,如果要填满一个Person的所有属性,可能需要读1句,或者多句,所以,person只能在循环外面实例化。
...全文
125 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2013-10-20
  • 打赏
  • 举报
回复
引用传递肯定会被修改,制作一个复本,跟据类的情况,可能涉及到深浅拷贝
threenewbee 2013-10-20
  • 打赏
  • 举报
回复
要么把Person定义成结构体 要么编写一个拷贝Person的代码(拷贝构造函数,静态工厂方法、Clone方法都可以)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧