Getting Started with Generics in RemObjects C#

We have several pages of code of advanced language features in Oxygene for Java on the PPS website and will use some of them as a starting point for demonstrations of RemObjects C# code. This example (for .Net) is a demonstration of generics converted from Oxygene code. It consists of one C# file for the main program and a file each for the generic stack (Stack.cs) and generic queue (Queue.cs).

To add the Stack.cs code file in Visual Studio:

  1. Select menu item Project > Add New Item;
  2. Select Code at the left of the Add New Item window;
  3. select New Code File;
  4. change the filename to Stack.cs;
  5. click on the Add button.

Code of the main program

namespace generic_stack_and_queue_cs { static class ConsoleApp { public static void Main(string[] args) { Stack<String> stringStack = new Stack<String>(); Queue<Integer> integerQueue = new Queue<Integer>(); String[] colours = new String[7]; stringStack.Push("violet"); stringStack.Push("indigo"); stringStack.Push("blue"); stringStack.Push("green"); stringStack.Push("yellow"); stringStack.Push("orange"); stringStack.Push("red"); Console.WriteLine(stringStack.Count().ToString() + " colours on stack"); for (Integer i = 0; i < 7; i++) { colours[i] = stringStack.Pop(); integerQueue.Push(i + 1); } Integer MaxCount = integerQueue.Count(); Console.WriteLine(MaxCount.ToString() + " integers in queue"); Console.WriteLine(); for (Integer k = 0; k < MaxCount; k++) { Console.WriteLine(integerQueue.Pop().ToString() + " " + colours[k]); } Console.ReadLine(); } } }

Code of Generic Stack

namespace generic_stack_and_queue_cs { public class StackItem<T> { public T Data { get; set;} public StackItem<T> Prev { get; set;} } public class Stack<T> { private StackItem<T> Top { get; set;} public Integer Count() { StackItem<T> last; Integer Result = 0; if (Top == null) return 0; last = Top; do { inc(Result); last = last.Prev; } while( last != null); return Result; } public T Pop() { if (Top == null) //throw new System.ArgumentException("Parameter cannot be null", "original"); throw new System.Exception("Empty Stack"); T Result = Top.Data; Top = Top.Prev; return Result; } public void Push(T aData) { StackItem<T> last ; last = Top; // may be null Top = new StackItem<T>(); Top.Data = aData; Top.Prev = last; } } }

Code of Generic Queue

namespace generic_stack_and_queue_cs { public class QueueItem<T> { public T Data{get;set;} public QueueItem<T> Next{get;set;} } public class Queue<T> { private QueueItem<T> First; public T Pop() { if (First == null) throw new System.Exception("Empty Queue"); else { T Result = First.Data; First = First.Next; return Result; } } public void Push(T aData) { QueueItem<T> last; if (First == null) { First = new QueueItem<T>(); First.Data = aData; } else { last = First; while (last.Next != null) { last = last.Next; } last.Next = new QueueItem<T>(); last.Next.Data = aData; } } public Integer Count() { QueueItem<T> last; Integer Result = 0; if (First == null) return 0; last = First; do { inc(Result); last = last.Next; } while (last != null); return Result; } } }