> For the complete documentation index, see [llms.txt](https://kinematicsoup.gitbook.io/reactor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinematicsoup.gitbook.io/reactor/api-reference/ks.reactor/ks.reactor.kshierarchyobject-1.md).

# Class ksHierarchyObject\<T>

Namespace: [KS.Reactor](/reactor/api-reference/ks.reactor.md)\
Assembly: KSCommon.dll

Templated base class for hierarchy objects that have a parent and list of children of type T. There is also a non-templated variant of ksHierarchyObject.

```csharp
public class ksHierarchyObject<T> where T : ksHierarchyObject<T>
```

#### Type Parameters

`T`

#### Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) ← [ksHierarchyObject\<T>](/reactor/api-reference/ks.reactor/ks.reactor.kshierarchyobject-1.md)

#### Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object\)), [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object-system-object\)), [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode), [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype), [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone), [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals), [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Constructors

### ksHierarchyObject() <a href="#ks_reactor_kshierarchyobject_1__ctor" id="ks_reactor_kshierarchyobject_1__ctor"></a>

Constructor.

```csharp
public ksHierarchyObject()
```

## Fields

### m\_children <a href="#ks_reactor_kshierarchyobject_1_m_children" id="ks_reactor_kshierarchyobject_1_m_children"></a>

```csharp
protected ksLinkedList<T> m_children
```

#### Field Value

[ksLinkedList](/reactor/api-reference/ks.reactor/ks.reactor.kslinkedlist-1.md)\<T>

### m\_parent <a href="#ks_reactor_kshierarchyobject_1_m_parent" id="ks_reactor_kshierarchyobject_1_m_parent"></a>

```csharp
protected T m_parent
```

#### Field Value

T

## Properties

### Ancestors <a href="#ks_reactor_kshierarchyobject_1_ancestors" id="ks_reactor_kshierarchyobject_1_ancestors"></a>

Iterator for ancestors.

```csharp
public IEnumerable<T> Ancestors { get; }
```

#### Property Value

[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1)\<T>

### Children <a href="#ks_reactor_kshierarchyobject_1_children" id="ks_reactor_kshierarchyobject_1_children"></a>

Children

```csharp
public ksConstList<T> Children { get; }
```

#### Property Value

[ksConstList](/reactor/api-reference/ks.reactor/ks.reactor.ksconstlist-1.md)\<T>

### Descendants <a href="#ks_reactor_kshierarchyobject_1_descendants" id="ks_reactor_kshierarchyobject_1_descendants"></a>

Iterator for descendants.

```csharp
public IEnumerable<T> Descendants { get; }
```

#### Property Value

[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1)\<T>

### Parent <a href="#ks_reactor_kshierarchyobject_1_parent" id="ks_reactor_kshierarchyobject_1_parent"></a>

Parent.

```csharp
public T Parent { get; }
```

#### Property Value

T

### Root <a href="#ks_reactor_kshierarchyobject_1_root" id="ks_reactor_kshierarchyobject_1_root"></a>

The root ancestor of this object, or this if this object is a root.

```csharp
public T Root { get; }
```

#### Property Value

T

### SelfAndAncestors <a href="#ks_reactor_kshierarchyobject_1_selfandancestors" id="ks_reactor_kshierarchyobject_1_selfandancestors"></a>

Iterator for this object and its ancestors.

```csharp
public IEnumerable<T> SelfAndAncestors { get; }
```

#### Property Value

[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1)\<T>

### SelfAndDescendants <a href="#ks_reactor_kshierarchyobject_1_selfanddescendants" id="ks_reactor_kshierarchyobject_1_selfanddescendants"></a>

Iterator for this object and its descendants.

```csharp
public IEnumerable<T> SelfAndDescendants { get; }
```

#### Property Value

[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1)\<T>

## Methods

### AddChild(T) <a href="#ks_reactor_kshierarchyobject_1_addchild__0" id="ks_reactor_kshierarchyobject_1_addchild__0"></a>

Adds a child to the object if that does not create a circular reference. If the child has another parent, removes it from its parent first. Throws ArgumentNullException if the child is null.

```csharp
public virtual bool AddChild(T child)
```

#### Parameters

`child` T

Child to add.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the child was added. False if it could not be added, either because it was already added or adding it would create a circular reference.

### Detach() <a href="#ks_reactor_kshierarchyobject_1_detach" id="ks_reactor_kshierarchyobject_1_detach"></a>

Removes the object from its parent. Does nothing if the object has no parent.

```csharp
public virtual void Detach()
```

### ForEachDescendant(ForEachCallback) <a href="#ks_reactor_kshierarchyobject_1_foreachdescendant_ks_reactor_kshierarchyobject__0__foreachcallback" id="ks_reactor_kshierarchyobject_1_foreachdescendant_ks_reactor_kshierarchyobject__0__foreachcallback"></a>

Iterates the descendants using depth-first search.

```csharp
public void ForEachDescendant(ksHierarchyObject<T>.ForEachCallback callback)
```

#### Parameters

`callback` [ksHierarchyObject](/reactor/api-reference/ks.reactor/ks.reactor.kshierarchyobject-1.md)\<T>.[ForEachCallback](/reactor/api-reference/ks.reactor/ks.reactor.kshierarchyobject-1/ks.reactor.kshierarchyobject-1.foreachcallback.md)

Callback to call on descendants. If it returns false, will not iterate children.

### ForSelfAndDescendants(ForEachCallback) <a href="#ks_reactor_kshierarchyobject_1_forselfanddescendants_ks_reactor_kshierarchyobject__0__foreachcallbac" id="ks_reactor_kshierarchyobject_1_forselfanddescendants_ks_reactor_kshierarchyobject__0__foreachcallbac"></a>

Iterates this object and its descendants using depth-first search.

```csharp
public void ForSelfAndDescendants(ksHierarchyObject<T>.ForEachCallback callback)
```

#### Parameters

`callback` [ksHierarchyObject](/reactor/api-reference/ks.reactor/ks.reactor.kshierarchyobject-1.md)\<T>.[ForEachCallback](/reactor/api-reference/ks.reactor/ks.reactor.kshierarchyobject-1/ks.reactor.kshierarchyobject-1.foreachcallback.md)

Callback to call on descendants. If it returns false, will not iterate children.

### GetChildIndex() <a href="#ks_reactor_kshierarchyobject_1_getchildindex" id="ks_reactor_kshierarchyobject_1_getchildindex"></a>

Gets the child index of this object in its parent's child list. Negative one for root objects.

```csharp
public int GetChildIndex()
```

#### Returns

[int](https://learn.microsoft.com/dotnet/api/system.int32)

Child index, or negative one if this is a root object.

### InsertChild(int, T) <a href="#ks_reactor_kshierarchyobject_1_insertchild_system_int32__0" id="ks_reactor_kshierarchyobject_1_insertchild_system_int32__0"></a>

Inserts a child at an index if that does not create a circular reference. If the child has another parent, removes it from its parent first. Throws ArgumentNullException if the child is null.

```csharp
public virtual bool InsertChild(int index, T child)
```

#### Parameters

`index` [int](https://learn.microsoft.com/dotnet/api/system.int32)

Index to insert at.

`child` T

Child to insert.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the child was inserted. False if it could not be inserted, either because it was already added, adding it would create a circular reference, or the index was out of bounds.

### IsDescendantOf(T) <a href="#ks_reactor_kshierarchyobject_1_isdescendantof__0" id="ks_reactor_kshierarchyobject_1_isdescendantof__0"></a>

Checks if an object is a descendant of this object.

```csharp
public bool IsDescendantOf(T obj)
```

#### Parameters

`obj` T

Object to check.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if obj is a descendant of this object.

### PerformAddChild(T) <a href="#ks_reactor_kshierarchyobject_1_performaddchild__0" id="ks_reactor_kshierarchyobject_1_performaddchild__0"></a>

Protected implementation of AddChild. The public AddChild calls this one. Derived classes can override either to change public or internal behaviour.

Adds a child to the object if that does not create a circular reference. If the child has another parent, removes it from its parent first. Throws ArgumentNullException if the child is null.

```csharp
protected virtual bool PerformAddChild(T child)
```

#### Parameters

`child` T

Child to add.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the child was added. False if it could not be added, either because it was already added or adding it would create a circular reference.

### PerformDetach() <a href="#ks_reactor_kshierarchyobject_1_performdetach" id="ks_reactor_kshierarchyobject_1_performdetach"></a>

Protected implementation of Detach.

Removes the object from its parent. Does nothing if the object has no parent.

```csharp
protected virtual void PerformDetach()
```

### PerformInsertChild(int, T) <a href="#ks_reactor_kshierarchyobject_1_performinsertchild_system_int32__0" id="ks_reactor_kshierarchyobject_1_performinsertchild_system_int32__0"></a>

Protected implementation of InsertChild. The public InsertChild calls this one. Derived classes can override either to change public or internal behaviour.

Inserts a child at an index if that does not create a circular reference. If the child has another parent, removes it from its parent first. Throws ArgumentNullException if the child is null.

```csharp
protected virtual bool PerformInsertChild(int index, T child)
```

#### Parameters

`index` [int](https://learn.microsoft.com/dotnet/api/system.int32)

Index to insert at.

`child` T

Child to insert.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the child was inserted. False if it could not be inserted, either because it was already added, adding it would create a circular reference, or the index was out of bounds.

### PerformRemoveChild(T) <a href="#ks_reactor_kshierarchyobject_1_performremovechild__0" id="ks_reactor_kshierarchyobject_1_performremovechild__0"></a>

Protected implementation of RemoveChild. The public RemoveChild calls this one. Derived classes can override either to change public or internal behaviour.

Removes a child from this object. Throws ArgumentNullException if child is null.

```csharp
protected virtual bool PerformRemoveChild(T child)
```

#### Parameters

`child` T

Child to remove.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the child was found and removed.

### RemoveChild(T) <a href="#ks_reactor_kshierarchyobject_1_removechild__0" id="ks_reactor_kshierarchyobject_1_removechild__0"></a>

Removes a child from this object. Throws ArgumentNullException if child is null.

```csharp
public virtual bool RemoveChild(T child)
```

#### Parameters

`child` T

Child to remove.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the child was found and removed.
