Could you think of a C# method accepting an ancestor, and forbidding a descendant of a class at compile time?
The answer to this probably is: why do you need such a reptile.
Well, I don't. I didn't meant to create such a method, but generics help a lot!
public class BinaryTreeNode<Node> where Node: BinaryTreeNode<Node> { public Node parent; public Node left; public Node right; } public class MyNode: BinaryTreeNode<MyNode> { public int key; } public class MyRoot: MyNode { } public class Test { public void test() { MyRoot root = new MyRoot(); // print((MyNode)root); // This works. print(root); // This does not work. } private static void print<T>(T node) where T: BinaryTreeNode<T> { Console.WriteLine("print me"); } }
By the way, BinaryTreeNode is an "abstract" class, as you cannot instantiate it but inherit only.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u