这是完全错误的。您的Displayer类不必要地一遍又一遍地更新控制台。每秒千次。这不合逻辑。
正确的方法是在需要时更新控制台。您的财产需要以另一种方式实施。
public class Displayer
{
private string[] _lines;
public string[] Lines
{
get { return _lines; }
set
{
// while setting new value call Update
_lines = value;
Update();
}
}
public async void Update()
{
// update console only once
Console.Clear();
foreach (string s in Lines)
{
Console.WriteLine(s);
}
}
}
如果您想了解收藏内的变化,请改为使用ObservableCollection
。
您不需要在此更改引用。既然你可以随时改变收藏的大小。
public class Displayer
{
public Displayer()
{
Lines = new ObservableCollection<string>();
Lines.CollectionChanged += Update; // Update will be called automatically when ever collection changes.
}
public ObservableCollection<string> Lines { get; }
private void Update(object sender, NotifyCollectionChangedEventArgs args)
{
// update console only once
Console.Clear();
foreach (string s in Lines)
{
Console.WriteLine(s);
}
}
}
为什么你认为你需要一个ref?你认为它会给你什么?你期望如何使用它? – ClickRick
@ClickRick,我需要使用异步方法来显示更改,这是通过更改int []变量完成的。 – SOCIOPATH
然后添加代码来说明。哦,并尝试没有参考。 – ClickRick