C# 检查 ref List<> 内容是否改变
C# check if ref List<> contents have changed
例如我在 class:
中有这个
List<int> myIntegerList;
public MyClass(ref List<int> intList)
{
myIntegerList= intList;
}
这是我的主要 class:
List<int> myIntegerList = new List<int>();
MyClass myNewClass;
for (int i = 0; i < 10; i++)
{
myIntegerList .Add(Random.Next(0, 100));
}
myNewClass = new MyClass(ref IntegerList);
如果引用的 List<int>
的内容发生变化,是否有一种简单的方法可以检查 myNewClass
对象?例如如果列表中的任何随机整数发生变化,则在 myNewClass
对象中引发一个事件。
使用 ObservableCollection..查看下面的内容link 以获得进一步的参考
List<T>
不会那样做,但 ObservableCollection<T>
会。另外,不要在构造函数中使用 ref
参数;任何引用 class 实例的 C# 变量都已经是一个引用。 class 类型的 ref
参数是一个引用 到 一个引用,这是你不想要的,可能也不想考虑。
using System.Collections.ObjectModel;
public class MyClass
{
private ObservableCollection<int> _integerList;
// Do not make this a ref parameter, it's a reference already
public MyClass(ObservableCollection<int> intList)
{
_integerList = intList;
_integerList.CollectionChanged += _integerList_CollectionChanged;
}
private void _integerList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Do stuff here in response to changed values in the list.
// Now back to the reference thing again: int is immutable.
// You can't change it, you can only replace it with another
// int. This event will be raised when you do that.
//
// If the objects in the ObservableCollection are class
// instances rather than ints, they're mutable. You can
// change their properties without replacing them.
// ObservableCollection cannot know when that happens, so
// it can't tell you.
//
// In that case, you'd need the class to implement
// INotifyPropertyChanged. That's a different can of worms,
// but it's a manageable one.
}
}
...
ObservableCollection<int> ints = new ObservableCollection<int>();
MyClass myNewClass;
var r = new Random();
for (int i = 0; i < 10; i++)
{
ints.Add(r.Next(0, 100));
}
myNewClass = new MyClass(ints);
例如我在 class:
中有这个List<int> myIntegerList;
public MyClass(ref List<int> intList)
{
myIntegerList= intList;
}
这是我的主要 class:
List<int> myIntegerList = new List<int>();
MyClass myNewClass;
for (int i = 0; i < 10; i++)
{
myIntegerList .Add(Random.Next(0, 100));
}
myNewClass = new MyClass(ref IntegerList);
如果引用的 List<int>
的内容发生变化,是否有一种简单的方法可以检查 myNewClass
对象?例如如果列表中的任何随机整数发生变化,则在 myNewClass
对象中引发一个事件。
使用 ObservableCollection..查看下面的内容link 以获得进一步的参考
List<T>
不会那样做,但 ObservableCollection<T>
会。另外,不要在构造函数中使用 ref
参数;任何引用 class 实例的 C# 变量都已经是一个引用。 class 类型的 ref
参数是一个引用 到 一个引用,这是你不想要的,可能也不想考虑。
using System.Collections.ObjectModel;
public class MyClass
{
private ObservableCollection<int> _integerList;
// Do not make this a ref parameter, it's a reference already
public MyClass(ObservableCollection<int> intList)
{
_integerList = intList;
_integerList.CollectionChanged += _integerList_CollectionChanged;
}
private void _integerList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Do stuff here in response to changed values in the list.
// Now back to the reference thing again: int is immutable.
// You can't change it, you can only replace it with another
// int. This event will be raised when you do that.
//
// If the objects in the ObservableCollection are class
// instances rather than ints, they're mutable. You can
// change their properties without replacing them.
// ObservableCollection cannot know when that happens, so
// it can't tell you.
//
// In that case, you'd need the class to implement
// INotifyPropertyChanged. That's a different can of worms,
// but it's a manageable one.
}
}
...
ObservableCollection<int> ints = new ObservableCollection<int>();
MyClass myNewClass;
var r = new Random();
for (int i = 0; i < 10; i++)
{
ints.Add(r.Next(0, 100));
}
myNewClass = new MyClass(ints);