Just put it in a static helper class like:
public static class CustomTreeNodeExtensions
{
public static IEnumerable<TreeNode> Descendants(this TreeNode root)
{
// method
}
}
Extensions are required to be in a static class.
BUT IF you create a CustomTreeNode
class anyhow why do you want it to be an extension method if you add it to the class directly? Why not make it a normal method (If you just created CustomTreeNode
for the extension this is irrelevant - in that case: the class containing the extension method is NOT required to inherit from the class you are trying to create the extension method for)?
public class CustomTreeNode : TreeNode
{
public IEnumerable<TreeNode> Descendants()
{
var nodes = new Stack<TreeNode>(new[] { this });
// rest
}
}