List<T>
不會做,但ObservableCollection<T>
意志。另外,不要在構造函數中使用ref
參數;任何引用類實例的C#變量都已經是引用了。 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);
使用'ObservableCollection',而不是'List'。另外,不要使用'ref'。在C#中,'myIntegerList' *已經*是一個對象的引用。只需傳入參考。 –
你不需要使用'ref'。你只需要,如果你想分配傳入的參數到別的東西,並且改變了你通過的變量。 – juharr