Reputation: 183
public interface IBox
{
IBox id { get; }
}
I am trying to understand the intention of IBox id
here when the interface is implemented on a class.
Why is IBox id
used here? From my understanding of interface, it lists the design requirement for the class. However, IBox id
is within IBox
itself. What are the possible uses of a class created with IBox
interface implemented?
I tried to create a class with the IBox
interface.
public class Box : IBox
{
public IBox id { get;}
}
What can be achieved by doing this?
Upvotes: 3
Views: 100
Reputation: 653
Here is what came to my mind first. Not sure if this answers your question though but let's say you are implementing a Linked list data structure. You will probably have some kind of a Node to represent the values in the list:
class LinkedList<INode>
{
public INode Head { get; set; }
}
interface INode
{
INode Next { get; set; }
}
class Node : INode
{
public object Value { get; set; }
public INode Next { get; set; }
}
The interface implementation can be used to ensure that the class will have a reference to itself in order to represent the next element in the linked list.
Upvotes: 6
Reputation: 37050
There are plenty of use-cases where a type may have a property of itself. Imagine a Person
. Every instance of that class may have Children
, which themeselves Person
s as well and may have other Children
.
class Person
{
public Person Child;
}
In your case one may argue that a box may live within another box or may contain other boxes.
Or let´s think of a Box
as a surrounding rectangle around some geometric figure. Of course that bounding-box is a geometry in itself, so it may have a bounding-box also (which however will be pretty much the same).
Upvotes: 3