Parametric polymorphism: Difference between revisions
Content added Content deleted
(J draft) |
|||
Line 60: | Line 60: | ||
<lang csharp>namespace RosettaCode { |
<lang csharp>namespace RosettaCode { |
||
class BinaryTree<T> { |
class BinaryTree<T> { |
||
T value; |
public T value; |
||
BinaryTree<T> left; |
public BinaryTree<T> left; |
||
BinaryTree<T> right; |
public BinaryTree<T> right; |
||
public |
public BinaryTree(T value) { |
||
this.value = value; |
this.value = value; |
||
} |
|||
public BinaryTree<U> Map<U>(Func<T,U> f) { |
|||
BinaryTree<U> Tree = new BinaryTree<U>(f(this.value)); |
|||
if (left != null) { |
if (left != null) { |
||
left. |
Tree.left = left.Map(f); |
||
} |
} |
||
if (right != null) { |
if (right != null) { |
||
right. |
Tree.right = right.Map(f); |
||
} |
} |
||
return Tree; |
|||
} |
} |
||
} |
} |
||
Line 81: | Line 86: | ||
class Program { |
class Program { |
||
static void Main(string[] args) { |
static void Main(string[] args) { |
||
BinaryTree< |
BinaryTree<U> b = new BinaryTree<int>(6); |
||
b.left = new BinaryTree<int>(5); |
|||
b.right = new BinaryTree<int>(7); |
|||
BinaryTree<U> b2 = b.Map(x => x * 10); |
|||
} |
} |
||
} |
} |