Inheritance with generic types can sometimes be tricky.

If you would inherit from a Stack<double>, no problem, but what if you want to keep it as generic as possible?

Maybe it's a bad idea, but I wrote something like `class Stacker<T> : Stack<T>`

and in Stacker I implemented some arithmetic methods like divide now if T is a double, all is well, but if T is a Person class? To divide two Persons would be hard to do. So our smart C# compiler does not let you use arithmetic operators here. This means T + T or T / T etc. are not allowed. Luckily the designers of C# thought of that too and introduced the `dynamic`

keyword. Which essentialy overrides the compiler checking in this case.

So before using arithmetic methods, it has become the resposability of the programmer to do the the checking here.

The extra methods I wrote are just some help in making life easier in working with a stack. I left most of the error checking out, to make it a bit clearer. Enjoy!

```
using System.Collections.Generic;
namespace StackTest
{
class Stacker<T> : Stack<T>
{
public Stacker()
{ }
/// <summary>
/// Duplicate top of stack: n -- nn
/// </summary>
public void DUP()
{
T v = this.Pop(); this.Push(v); this.Push(v);
}
/// <summary>
/// Multiply(if possible) the two top items on the stack
/// replace with the result
/// </summary>
public void MUL()
{
if (Count >= 2)
{
dynamic v1 = Pop();
dynamic v2 = Pop();
Push(v1 * v2);
}
}
/// <summary>
/// Sum(if possible) the two top items on the stack
/// replace with the result
/// </summary>
public void ADD()
{
if (Count >= 2)
{
dynamic v1 = Pop();
dynamic v2 = Pop();
Push(v1 + v2);
}
}
/// <summary>
/// Substract(if possible) the two top items on the stack
/// replace with the result
/// </summary>
public void SUB()
{
if (Count >= 2)
{
dynamic v1 = Pop();
dynamic v2 = Pop();
Push(v2 - v1);
}
}
/// <summary>
/// Divide(if possible) the two top items on the stack
/// replace with the result
/// </summary>
public void DIV()
{
if (Count >= 2)
{
dynamic v1 = Pop();
dynamic v2 = Pop();
Push(v1 / v2);
}
}
/// <summary>
/// Negate(if possible) the top item on the stack
/// replace with the result
/// </summary>
public void NEG()
{
dynamic v1 = Pop();
Push(-v1);
}
/// <summary>
/// Rotate the 3 th item to the top of the stack: n1 n2 n3 -- n2 n3 n1
/// </summary>
public void ROT()
{
if (Count >= 3)
{
dynamic v3 = Pop();
dynamic v2 = Pop();
dynamic v1 = Pop();
Push(v2);
Push(v3);
Push(v1);
}
}
/// <summary>
/// Remove top item
/// </summary>
public void DROP()
{
T v = Pop();
}
/// <summary>
/// Swap the two topmost items on the stack:n1 n2 -- n2 n1
/// </summary>
public void SWAP()
{
if (Count >= 2)
{
T v1 = Pop();
T v2 = Pop();
Push(v1);
Push(v2);
}
}
/// <summary>
/// Copy the second item and make it the topmost:n1 n2 -- n1 n2 n1
/// </summary>
public void OVER()
{
if (Count >= 2)
{
T v1 = Pop();
T v2 = Pop();
Push(v1);
Push(v2);
Push(v1);
}
}
}
}
// A a sample of possible use:
using System;
namespace StackTest
{
class Program
{
static void Main(string[] args)
{
Stacker<double> SD = new Stacker<double>();
SD.Push(3.0);
SD.DUP();
SD.MUL();
Console.WriteLine("The square of 3 is {0}.", SD.Peek());
Console.WriteLine("Evaluation of 5*(4-2)");
SD.Push(2.0);
SD.Push(5.0);
SD.Push(4.0);
SD.ROT();
SD.SUB();
SD.MUL();
Console.WriteLine("The result is {0}.", SD.Peek());
Console.ReadKey();
}
}
}
```

About the Author

Have programmed in Modula-2 on a professional basis in the eighties. Now I am quite fond of C# AND Python!