如何ObservableCollection里的数组值保持不变?
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只能在循环外面实例化。 --------------------编程问答-------------------- 要么把Person定义成结构体
要么编写一个拷贝Person的代码(拷贝构造函数,静态工厂方法、Clone方法都可以) --------------------编程问答-------------------- 引用传递肯定会被修改,制作一个复本,跟据类的情况,可能涉及到深浅拷贝
补充:.NET技术 , C#