例如,這兩種方法需要更新count
變量:是否存在強制執行操作的C#屬性?
public void AddFront(T data) {
Node<T> newNode = new Node<T>(data, null, null);
if (count == 0) {
head = tail = newNode;
}
else {
newNode.Next = head;
head.Previous = newNode;
head = newNode;
}
// don't forget
count++;
}
public void AddBack(T data) {
Node<T> newNode = new Node<T>(data, null, null);
if (count == 0) {
head = tail = newNode;
}
else {
newNode.Previous = tail;
tail.Next = newNode;
tail = newNode;
}
// don't forget
count++;
}
不知是有可以強制operation
這樣發生的屬性?,否則會觸發一個編譯時間錯誤消息。
你要找的代碼契約: http://research.microsoft.com/en-us/projects/contracts/ – 2012-07-25 22:14:09
在這裏,你會想'Contract.Ensures (Contract.ValueAtReturn(out count)== Contract.OldValue(count)+1);' – 2012-07-25 22:15:36
有趣!感謝您的信息,現在就看看。 – Chan 2012-07-25 22:16:24