什麼是 C# 中的迴圈引用?


當兩個或多個相互依賴的資源造成鎖定條件時,就會出現迴圈引用。這會導致資源不可用。

要處理 C# 中的迴圈引用問題,應使用垃圾回收。它會檢測並收集迴圈引用。垃圾回收器從區域性變數和靜態變數開始,並標記每個可以透過其子項訪問的物件。

透過此方法,你可以處理迴圈引用問題。

假設以下類處於迴圈引用中。在這裡,它們都互相依賴:

public class A {
   B Two;
}

public class B {
   A one;
}

要解決此問題,建立介面:

public interface myInterface {
}

public class A {
   myInterface Two;
}

public class B: myInterface {
   A one;
}

更新日期:2020 年 6 月 20 日

3K+ 瀏覽次數

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.