What are circular references in C#?


Circular reference occurs when two or more interdependent resources cause lock condition. This makes the resource unusable.

To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references. The garbage collector begins with local and static and it marks each object that can be reached through their children.

Through this, you can handle the issues with circular references.

Let’s say the following classes is in circular reference. Here both of them depends on each other −

public class A {
   B Two;
}

public class B {
   A one;
}

To solve the issue, create an interface −

public interface myInterface {
}

public class A {
   myInterface Two;
}

public class B: myInterface {
   A one;
}

Updated on: 20-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements