3

我有以下课程:

public abstract class BaseGridViewModel
{
    protected BaseGridViewModel()
    {
        Timer = new List<long>();
    }
    public List<long> Timer { get; set; }
}

要更新计时器,我有:

vm.Timer.Add(sw.ElapsedMilliseconds);

我对列表不太熟悉。我是否有可能拥有一个包含长字符串和字符串的列表,以便我可以执行以下操作:

vm.Timer.Add(sw.ElapsedMilliseconds, "After event 1");
vm.Timer.Add(sw.ElapsedMilliseconds, "After event 2");
4

2 回答 2

7

创建一个类来保存这两个值,并将它们存储在列表中。

public sealed class TimeEvent
{
  public long Elapsed {get; private set;}
  public string Description { get; private set;}
  public TimeEvent(long elapsedTime, string descriptionOfEvent)
  {
    this.Elapsed = elapsedTime;
    this.Description = descriptionOfEvent;
  }
}

public IList<TimeEvent> TimerEvents {get; set;}

vm.Timer.Add(new TimeEvent(sw.ElapsedMillisends, "After event 1"));
于 2012-09-12T22:33:42.827 回答
2

您可以创建一个class,然后创建List一个class

public class TimerData
{
    public long time;
    public string description;

    public TimerData(long t, string d)
    {
        this.time = t;
        this.description = d;
    }
} 


List<EventData> list = new List<EventData>();
list.Add(new TimerData(50, "Description"));

你可以用你的类做更高级的事情,但是这个方法很简单。

于 2012-09-12T22:37:31.917 回答