我打算通過下面這段代碼:爲什麼額外的if語句在調用它之前檢查一個事件是否爲空?
public delegate void PersonArrivedEventHandler(string personName, byte[] personId);
public class SCLib
{
public event personArrivedEventHandler personrrived;
public SCLib()
{
// Simulate that the person arrived after 2000 milli-seconds.
Task.Run(() =>
{
System.Threading.Thread.Sleep(2000);
OnPersonArrived("personName", new byte[] { 0x20, 0x21, 0x22 });
});
}
protected virtual void OnPersonArrived(string smartCardReaderName, byte[] smartCardId)
{
if (this.personArrived != null)
{
PersonArrived(personName, personId);
}
}
}
但是,我不知道這是什麼線的意義, if (this.personArrived != null)
。
爲什麼在這裏完成此檢查?這裏有if statement
有什麼意義嗎?我刪除了這一行並運行該程序,一切都如以前那樣運行。
謝謝。
「我剛從房子裏取出所有的煙霧探測器,其中的所有東西都像以前一樣工作,所以我猜煙霧探測器是不必要的。否:除煙霧探測器之外的所有東西都像以前一樣工作。當您卸下安全系統不再運行的安全系統時。 –
@EricLippert:很好的比喻。有道理。 –