class Transaction { // Private field holding the time of
// the last transaction performed
private DateTime timeOfLastTransaction; public void PerformTransaction() {
// Lock this object
Monitor.Enter(this); // Perform the transaction... // Record time of the most recent transaction
timeOfLastTransaction = DateTime.Now; // Unlock this object
Monitor.Exit(this);
} // Public read-only property returning
// the time of the last transaction
public DateTime LastTransaction {
get {
// Lock this object
Monitor.Enter(this); // Save the time of the last transaction
// in a temporary variable
DateTime dt = timeOfLastTransaction; // Unlock this object
Monitor.Exit(this); // Return the value in the temporary variable
return(dt);
}
}
}
-----------------------------------------------------------------------------------
上面这段代码为什么要使用临时变量 dt ?
// the last transaction performed
private DateTime timeOfLastTransaction; public void PerformTransaction() {
// Lock this object
Monitor.Enter(this); // Perform the transaction... // Record time of the most recent transaction
timeOfLastTransaction = DateTime.Now; // Unlock this object
Monitor.Exit(this);
} // Public read-only property returning
// the time of the last transaction
public DateTime LastTransaction {
get {
// Lock this object
Monitor.Enter(this); // Save the time of the last transaction
// in a temporary variable
DateTime dt = timeOfLastTransaction; // Unlock this object
Monitor.Exit(this); // Return the value in the temporary variable
return(dt);
}
}
}
-----------------------------------------------------------------------------------
上面这段代码为什么要使用临时变量 dt ?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货