我有一个像Quete(先进先出)的课程
public class Tor<T>
{
private T[] elements;
private int next;
public event EventHandler<EventArgs> queueFull;
}
我需要构建一个构造函数来获取一个参数(队列大小(int)。并且它分配arrary并初始化变量,
怎么做?
答案 0 :(得分:2)
简单的
public Tor(int size)
{
elements = new T[size];
}
答案 1 :(得分:1)
public class Tor<T> where T : new()
{
public Tor(int size)
{
elements = new T[size];
for (int i = 0; i < size; i++)
{
elements[i] = new T();
}
}
private T[] elements;
private int next;
public event EventHandler<EventArgs> queueFull;
}
或
public class Tor<T> where T : new()
{
public Tor(int size)
{
elements = Enumerable.Range(1,size).Select (e => new T()).ToArray();
}
...
}
答案 2 :(得分:0)
public class Tor<T>
{
private T[] elements;
private int next;
public event EventHandler<EventArgs> queueFull;
public Tor(int capacity)
{
elements = new T[capacity];
}
}
应该这样做。
答案 3 :(得分:0)
public class Tor<T>
{
private T[] elements;
private int next;
public event EventHandler<EventArgs> queueFull;
public Tor(int size)
{
if (size < 0)
throw new ArgumentOutOfRangeException("Size cannot be less then zero");
elements = new T[size];
next = 0;
}
}
答案 4 :(得分:0)
考虑使用现有的课程