Brendan Enrick

Daily Software Development

Visual Studio 2008 and the .NET Framework 3.5 Released

In my attempt to add to the monotony, I'll say that not long ago Visual Studio 2008 was released to all of the MSDN subscribes. With the news spreading so quickly the downloads are all taking forever. Good luck! Scott Guthrie, as usual, posted some good content about this Visual Studio 2008 release. There will be some trial versions of the professional available in the not too distant future. You can also read a nice tour of all of the new features added into this new release. Scott wrote short overviews for a lot of the information and linked to more in-depth descriptions of these features. Make sure you follow those links if you're not well informed about this release.

The Visual Studio Express Editions and  the .NET Framework 3.5 runtime are currently available for download. These downloads should work better than the MSDN full version of Visual Studio. These are not multiple gigabyte files and also are probably not as popular right now.

I've been hearing complaints all day about difficulties trying to download it. I am assuming the problems result from too many users attempting to download. Good luck and enjoy some of the great new features.

Visual Studio Extensibility

While I was at DevConnections last week, I watched an interesting demo showing off Visual Studio's extensibility. In the demo the visual studio shell is being used to edit Lua script; the scripting language used by World of Warcraft for designing custom interfaces. The demo showcased Visual Studio's flexibility and captured the audience's attention pretty well.

The speaker added an interesting interface into World of Warcraft which showed images and played sounds when she killed enemies. I think it will be nice to use Visual Studio for almost any programming language. The familiar environment will make things easier. Perhaps more add-ons for Visual Studio will be available in the near future. This WoW Lua add-on is supposed to be available on CodePlex soon.

I don't play World of Warcraft, so I will be waiting to see what extensions other people will decide to make.

SimpleCMS on CodePlex

A couple of weeks ago Steve Smith and I released SimpleCMS as an open source project on CodePlex. SimpleCMS is, as the name suggests, a Simple Content Management System. It is a small ASP.NET 2.0 Plug-in which allows dynamic creation of simple pages. I've not gotten much opportunity to work on it in a long time, but I did manage to get enough time to put it on CodePlex. 

The main goal of SimpleCMS is to be simple. It isn't designed to support a lot of features or be bloated. It is designed to be lightweight. I recommend checking it out if you get the chance. The project could use some more development, and if I get the free time I'll try to work on it a bit.

Let me know what you think about SimpleCMS.

Memory Management: Generics vs objects

One of the most important parts of software development is memory management. Memory management is important for every software application. If one is to write a well developed software application, one must have a fair bit of knowledge in the area of memory managament. It is important to understand the underlying technology which allows programs to function. There are many aspects of programs which come into play often in .NET applications; variables, functions, garbage collection.

Understanding Garbage Collection, Heaps, and Stacks

When using a language with a heap, it is important to understand that memory will be allocated dynamically. This is separate from where the basic program memory resides; the stack. On the stack memory builds as the program executes. Local variables are stored with the function information. This is the memory used in pretty much every language. When a heap is involved there is basically an area of memory where variables may be dynamically defined and only references to these variables are stored as pointers on the stack.

When the variable is out of scope or removed it is merely the reference to the variable which is removed. A garbage collector will come by later to free the memory for later use. Whenever there is not enough room to allocate memory in the heap the garbage collector wil come and clear unused memory.

Understanding Variable Types

There are basically two types of variables; value and reference. The value types tend to be the simpler variables and the reference types tend to be the more advanced variables. Some examples of value types are enums, ints, doubles, bools, chars, and structs. Some reference types are arrays, lists, and classes. The value types are the ones stored locally with the stack and the reference types are stored in the heap, and a reference to them is stored on the stack. When using the new keyword, memory is allocated on the heap and a referenced object is created.

Passing Parameters to Functions.

When passing variables as parameters to functions, it is important to understand the two main ways in which these variables may be passed. Variables may be passed by reference or by value. Passing by value means that we make a copy of the local variable and use that in the function. This means that our value type variable will be copied and if we use a reference type it means that only the reference will be copied. This means that we will still be accessing the same location in the heap even if we pass by value. By using the ref keyword you may pass a parameter by reference. This means for a value type you are able to just pass a reference to the original value. Do not pass a reference type by reference, because it will just make a reference to the reference to the value and that will slow things down for no reason. Passing by reference is a good idea if the value is large, so with a large struct you may want to pass by reference instead of copying all of the data.

Generics vs. Objects

When using parameters of type object, boxing and unboxing is used to take the variable and changing it to and from object. This seems pretty easy because one can simply add (object) when passing the parameter. The problem here is that this will create a new instance of an object. This means we will be making a copy of the variable instead of just using a reference to the variable. This will take time, and will also allocate extra memory which will need to be collected by the garbage collector later. This will also decrease the performance of our program. This is why it is important to use generics to prevent this performance hit. When we use generics we do not have to make a copy of the value, we merely pass a reference to the original value.

Be careful, and try to use Generics whenever possible. Your memory will thank you.

Table Variables vs. Temporary Tables in SQL

Temporary tables and table variables can be used in pretty much the same way. One question which is asked, "Which one is better". To answer that it is important to understand a few of the differences between the two. Once we understand how these two types of tables are different we will better understand which we need to use when.

Creating a temporary table is very similar to creating a regular table. In order to do this we use the following code

create table #myTempTable (columnA typeA, columnB typeB, ...)

Table variables are created as regular variables are declared. The type of the variable is just defined as a table.

declare @myTableVariable table (columnA typeA, columnB typeB, ...)
Scope is another important factor to consider. With a table variable its scope is similar to other variables. A temporary table however does not share this scope, and can even be accessed in a stored procedure called by your code. This is one very nice feature of temporary tables over table variables.

Table variables do not record any entries in transaction logs. Temporary tables do store their transactions. Depending on what you are trying to accomplish this can be a benefit for either of the two types of tables. It is often very important to keep track of the transaction log, and in these instances a temporary table is far better for your needs, but other times the transaction log is simply unnecessary and a table variable would be the optimal choice.

I've heard of great performance gains in temporary tables because of their ability to be pre-compiled. I've also heard that table variables are faster than temporary tables in general. I believe this has to do with the nicely defined scope of table variables. Because of this they may use fewer resources than temporary tables. An example of this is the transaction log I mentioned earlier.

For the most part a table variable is just as flexible as a temporary table, and in most instances it also out performs temporary tables in my experience.

One thing to watch out for when using temporary tables is the possibility of causing your store procedure to recompile. Microsoft has a Knowledge Base Article about Troubleshooting stored procedure recompilation which discusses this danger. Table variables will not cause this problem.

I would recommend the use of table variables for everyday use, and only using temporary tables when they're required. Instances of this include the need for a transaction log. Or any time you feel like writing extra lines of code simply to perform cleanup on your temporary table. But that is just my $0.02 on this issue.

Happy SQL writing!

Accessing Controls inside of Templated Controls

One question that seems to come up often in the asp.net forums is from people who are trying to access controls within controls using templates. LoginViews and CreateUserWizard controls are two commonly used templated controls. These templated controls don't actually have their contents known until run-time because it is dependant on something else; data from a database, user permissions, etc.

Since this information is not known you can't just access the inner controls as you would normal controls, because it is not known until run-time what the controls are. You will get compiler errors if you try to access them. The easiest way to get the controls you need is to use a recursive find control function. Steve Smith has blogged about Using a Recursive Find Control as well as about a very important Code Optimization for using a recursive find control.

The cool thing about the recursive find control is that it will dig down into the controls collection of a control you specify looking for the control you are looking for. It is great for templated controls, because it is a lot neater than trying to statically go after it like this.

Bad Code Do Not Use This

Label Label1 = LoginView1.Controls[2].Controls[2].Controls[1].Controls[3].FindControl("Label1") as Label;

The problem with the above code is that if you move anything you'll break the code. It is not very stable, and is even hard to tell what is being done. Do not worry there is a better way of handling this. A recursive find control is an expensive operation, and this is why Steve's code optimization is important. You don't want to execute the find control more times than needed.

Good Code Use This

public static System.Web.UI.Control FindControl(System.Web.UI.Control start, string id)
{
    System.Web.UI.Control foundControl;
    if (start != null)
    {
        foundControl = start.FindControl(id);
        if (foundControl != null)
            return foundControl;
        foreach (Control c in start.Controls)
        {
            foundControl = FindControl(c, id);
            if (foundControl != null)
            return foundControl;
        }
    }
    return null;
}

The above code will find controls nested within templated controls for you. This code will probably go in one of your class libraries where you keep utility functions. When you're calling this function, I would recommend you call it within a property as in Steve's optimization. The following code shows how you would do the previous example in a better way.

Good Code Use This

private Label _label1 = null;
private Label Label1
{
    get
    {
        if (_label1 == null)
        {
            _label1 = Utilities.FindControl(LoginView1, "Label1") as Label;
        }
        return _label1 ;
    }
}

This allows you to access the control as you normally would try. You can just type in the name of the property and it will let you use it as if it were the control and there were no templated control there at all. Makes working with LoginViews and CreateUserWizard controls.

Repeating templated controls such as the GridView can also benefit from this functionality, but you need to be more careful with them because there is a control with the name you're looking for in each row of the repeating control. Make sure when using this there that you get the correct row first, and only search within that row.

Have fun finding your controls.

Public Strongly Typed Resource Generator

I recently installed an interesting custom tool. It is an Extended version of the resource generator in Visual Studio. The nicest part of this for me is that this one is public instead of internal like the default one. In order to have a centralized resource file it needs to be usable between projects in Visual Studio, but the internal class made this difficult. I could just manually update the generated code each time after using the file, but that would just be stupid.

This is a great answer to the problem I needed to solve because it allows me to keep the resource file in one spot and not have to do and weird tricks to accomplish my goals.

The article accompanying this tool is well-written and explains a bit about how to use and install it.

This little tool is easy to install. It comes with an MSI installer or the source code whichever you prefer. I'd say just go with the MSI.

Note: Make sure if you are on Vista you run it as administrator though or it will not work. Since it is an MSI file you will not see an option to run as administrator, so you will want to create a batch file to do this. In the batch file you will execute the MSI file. You will run the batch file as administrator. The following code should be in the batch file.

msiexec /i "Absolute path to the installer \ResXFileCodeGeneratorEx.msi"

That should get it installed. You then need to create a resource file and in the "Custom Tool" field of the properties window type "ResXFileCodeGeneratorEx" into the field. This will then make your new generated Strongly Typed resources a public class.

Congratulations you may now use your resource file between projects!

Understanding the Queue Data Structure Using a Simple C# Implementation

Introduction

Queue

The Queue is a very important and common Data Structure in Computer Science. As the name queue suggests, this data structure can be described with an analogy of waiting in a line. There is no cutting allowed in the line, so the first person in the line is the first person out of the line. With the data structure it is the same way First In First Out (FIFO).

The best way of managing the data in a queue in my opinion is by storing the data in a "circle" by this I mean that when we reach the end of the contiguous data if we have room at the beginning we can start filling in there. This data is stored kind of like a snake going through a circular pathway. The snake will grow and shrink as it travels along this path.

Methods of a Queue

There are only two functions which are required with a queue. One will insert data into the queue and one will remove data from the queue. (One to grow the snake and one to shrink the snake) We call these two functions Enqueue and Dequeue.

  • Enqueue is our method for adding new items to the queue. It will add the new items to the back of the queue.
  • Dequeue is our method for removing items from the queue. It will remove items from the front of the queue.

Since I like my collections to not be limited in size, we will also have a private function for increasing the size of the queue if we run out of space and need to add another element.

Common Implementations

Generally a queue will be an array and it will have a pointer to the front of the queue and a pointer to the end of the queue. As new items are added and removed the pointers will adjust themselves. The pointer to the end of the queue is the pointer to the location where the next element will be placed into the queue, and the pointer to the front of the queue is the location of the next element which will come out of the queue.

In my example I have simplified this a little bit by not using pointers. I'll just be using a FrontIndex and a BackIndex which are just integers telling me which index to use for my array.

Simple Sample C# Implementation

As I like doing, this queue will be a Generic queue, so this is how it will be defined.

public class Queue<T>
{
}

With the class defined, I'll start by adding the following properties.

#region Properties
 
/// <summary>
/// The capacity of the Elements Collection
/// </summary>
private int _capacity;
public int Capacity
{
    get { return _capacity; }
    set { _capacity = value; }
}
 
/// <summary>
/// The number of elements currently in the queue.
/// </summary>
private int _length;
public int Length
{
    get { return _length; }
    set { _length = value; }
}
 
/// <summary>
/// The actual data elements stored in the queue.
/// </summary>
private T[] _elements;
protected T[] Elements
{
    get { return _elements; }
    set { _elements = value; }
}
 
/// <summary>
/// This is the index where we will dequeue.
/// </summary>
private int _frontIndex;
public int FrontIndex
{
    get { return _frontIndex; }
    set { _frontIndex = value; }
}
 
/// <summary>
/// This is the index where we will next enqueue a value. 
/// It is calculated based on the Front Index, Length, and Capacity
/// </summary>
public int BackIndex
{
    get { return (FrontIndex + Length) % Capacity; }
}
 
#endregion

Once I have these properties in place I'll add my basic constructors; one which takes no parameters and one which takes a capacity.

#region Constructors
 
public Queue()
{
    Elements = new T[Capacity];
}
 
public Queue(int capacity)
{
    Capacity = capacity;
    Elements = new T[Capacity];
}
 
#endregion

Now that I am able to create the queue, it is time to start adding the functionality. As I've mentioned, we need to have enqueue, dequeue, and the private method IncreaseCapacity.

With Enqueue I'll have it take in an element and then check to see if we have space. If we don't I'll increase the capacity, and if we do I'll add the element at the back index and increment the length. Since we are calculating BackIndex based on the FrontIndex and the length we don't have to manually adjust it.

Dequeue will make sure we are not empty and if we are it will throw an error. We then want to pull the next element out of the front of the queue and adjust our length accordingly. We also want to increment our FrontIndex. Notice that we increment it by 1 and mod it by the Capacity. This gives us the remainder of FrontIndex + 1 / Capacity. This gives us our circular storage. It makes it so when we read the end of the array we start back at the beginning.

#region public methods
 
public void Enqueue(T element)
{
    if (this.Length == Capacity)
    {
        IncreaseCapacity();
    }
    Elements[BackIndex] = element;
    Length++;
}
 
public T Dequeue()
{
    if (this.Length < 1)
    {
        throw new InvalidOperationException("Queue is empty");
    }
    T element = Elements[FrontIndex];
    Elements[FrontIndex] = default(T);
    Length--;
    FrontIndex = (FrontIndex + 1) % Capacity;
    return element;
}
 
#endregion

Increasing the capacity is not very difficult. In this example I am going to add 1 and double the capacity when it needs to be increased. I like the idea of hitting a reset button on the queue every time we increase size, so I'll create a new queue and enqueue into it every element from the current queue using the dequeue method to empty the current one. I'll then assign all of the properties of the new queue to the current one and I am done.

#region protected methods
 
protected void IncreaseCapacity()
{
    this.Capacity++;
    this.Capacity *= 2;
    Queue<T> tempQueue = new Queue<T>(this.Capacity);
    while (this.Length > 0)
    {
        tempQueue.Enqueue(this.Dequeue());
    }
    this.Elements = tempQueue.Elements;
    this.Length = tempQueue.Length;
    this.FrontIndex = tempQueue.FrontIndex;
}
 
#endregion

With this function we should now have a working Queue structure. To test it we can run it through some quick little for loops.

Queue<int> myQueue = new Queue<int>();
for (int i = 0; i < 50; i++)
{
    Console.WriteLine("Enqueue: " + i);
    myQueue.Enqueue(i);
    Console.WriteLine("New Length is: " + myQueue.Length);
}
 
for (int i = 0; i < 50; i++)
{
    Console.WriteLine("Dequeue: " + myQueue.Dequeue());
    Console.WriteLine("New Length is: " + myQueue.Length);
}
 
for (int i = 0; i < 50; i++)
{
    Console.WriteLine("Enqueue: " + i);
    myQueue.Enqueue(i);
    Console.WriteLine("New Length is: " + myQueue.Length);
}
 
for (int i = 0; i < 50; i++)
{
    Console.WriteLine("Dequeue: " + myQueue.Dequeue());
    Console.WriteLine("New Length is: " + myQueue.Length);
}
 
try
{
    myQueue.Dequeue();
}
catch (InvalidOperationException ex)
{
    Console.WriteLine("As expected I received this error: " + ex.Message);
}

Once we run that and see that it works we know that we probably have a Queue that works pretty well.

Once again here is the entire Queue Class.

public class Queue<T>
{
    #region Properties
 
    /// <summary>
    /// The capacity of the Elements Collection
    /// </summary>
    private int _capacity;
    public int Capacity
    {
        get { return _capacity; }
        set { _capacity = value; }
    }
 
    /// <summary>
    /// The number of elements currently in the queue.
    /// </summary>
    private int _length;
    public int Length
    {
        get { return _length; }
        set { _length = value; }
    }
 
    /// <summary>
    /// The actual data elements stored in the queue.
    /// </summary>
    private T[] _elements;
    protected T[] Elements
    {
        get { return _elements; }
        set { _elements = value; }
    }
 
    /// <summary>
    /// This is the index where we will dequeue.
    /// </summary>
    private int _frontIndex;
    public int FrontIndex
    {
        get { return _frontIndex; }
        set { _frontIndex = value; }
    }
 
    /// <summary>
    /// This is the index where we will next enqueue a value. 
    /// It is calculated based on the Front Index, Length, and Capacity
    /// </summary>
    public int BackIndex
    {
        get { return (FrontIndex + Length) % Capacity; }
    }
 
    #endregion
 
    #region Constructors
 
    public Queue()
    {
        Elements = new T[Capacity];
    }
 
    public Queue(int capacity)
    {
        Capacity = capacity;
        Elements = new T[Capacity];
    }
 
    #endregion
 
    #region public methods
 
    public void Enqueue(T element)
    {
        if (this.Length == Capacity)
        {
            IncreaseCapacity();
        }
        Elements[BackIndex] = element;
        Length++;
    }
 
    public T Dequeue()
    {
        if (this.Length < 1)
        {
            throw new InvalidOperationException("Queue is empty");
        }
        T element = Elements[FrontIndex];
        Elements[FrontIndex] = default(T);
        Length--;
        FrontIndex = (FrontIndex + 1) % Capacity;
        return element;
    }
 
    #endregion
 
    #region protected methods
 
    protected void IncreaseCapacity()
    {
        this.Capacity++;
        this.Capacity *= 2;
        Queue<T> tempQueue = new Queue<T>(this.Capacity);
        while (this.Length > 0)
        {
            tempQueue.Enqueue(this.Dequeue());
        }
        this.Elements = tempQueue.Elements;
        this.Length = tempQueue.Length;
        this.FrontIndex = tempQueue.FrontIndex;
    }
 
    #endregion
}

Happy Data Structure Building!

Using a Numbers Table to Iterate Over Dates

I recently wrote some SQL which would do some work for a single day, but I wanted my code to be able to run once for each day in a date range. Luckily for me, I read Gregg Stark's SQL Blog. He recently posted about how to Iterate Over a List of Dates using a Numbers table in SQL. The SQL he gives is pretty much a cookie cutter solution to the problem.

The basic idea of this trick is to have a cursor which is going to allow you to fetch each of the dates into a variable for use in your code.

You first need to have a Numbers table in your database. This is basically just a table which contains a single number for each row. Use the following short snippet to create your Numbers table.

 
CREATE TABLE dbo.Numbers
(
    Number int IDENTITY(1, 1) PRIMARY KEY
)
GO
 
declare @MaxNumber int
set @MaxNumber = 65535
WHILE 1 = 1
BEGIN
    INSERT INTO dbo.Numbers DEFAULT VALUES
    
    IF scope_identity() = @MaxNumber 
    BEGIN
        BREAK
    END
END
GO

Once you have this numbers table you can follow this cookie cutter solution Gregg created which is actually a very nicely written solution to the problem.

Declare @StartDate datetime
Declare @EndDate datetime
 
-- Note: This StartDate is Exclusive and this EndDate is Inclusive
set @StartDate = '10/1/2007'
set @EndDate = '10/9/2007'
 
declare @CurrentDate datetime
 
-- Create the cursor with the dates using the numbers table
declare datecursor cursor forward_only 
    for Select dateadd(d,Number,@StartDate) from Numbers
          where Number <= datediff(d, @StartDate, @EndDate)
          order by Number
open datecursor
 
-- Loop which will exit when we are out of dates to check
while (1=1)
begin
    fetch next from datecursor into @CurrentDate 
    if @@fetch_status <> 0
        break;
    
    -- This is the code which will run for each date
    select * from some_table where DateRecorded = @CurrentDate
 
end
 
-- Cursor Cleanup
close datecursor
deallocate datecursor

Happy SQL writing!

Simple C# Stack Implementation

I've just written a quick little stack implementation for use in explaining the stack data structure. It is written in C# and is not exactly a robust solution, but it works well enough. It is stubbed out with the basics of what is required to have a working stack. If you don't know how a Stack works, I also recommend reading my Simple Explanation of the Stack Data Structure.

For the heck of it, since I love Generics, I decided to make this a Generic Stack. The first step is to create the Generic class definition. To make it Generic we will add <T> into the name of the class.

public class Stack<T>
{
}

Once we have this class we will want to give it some properties. The properties I am going to use for this implementation are; Capacity to hold the amount of space reserved for the Stack, Length to let us know how many elements are currently in our stack, Elements to actually contain our elements, and Index to keep track of which element is currently on the top of the stack.

#region Properties
 
private int _capacity;
public int Capacity
{
    get { return _capacity; }
    set { _capacity = value; }
}
 
public int Length
{
    get { return Index + 1; }
}
 
private T[] _elements;
protected T[] Elements
{
    get { return _elements; }
    set { _elements = value; }
}
 
private int _index = -1;
public int Index
{
    get { return _index; }
    set { _index = value; }
}
 
#endregion

We have initialized Index to -1 and Length is always Index + 1, so right now Length is 0. Notice that Elements is an array of T elements. When someone instantiates this stack class T will be defined as some type.

In our constructor we will want to initialize Elements to be an empty array for us to store out data. For this simple example I have two constructors. One of them takes 0 parameters and creates an empty array, and the other constructors takes an initial capacity for the array.

public Stack()
{
    Elements = new T[Capacity];
}
 
public Stack(int capacity)
{
    Capacity = capacity;
    Elements = new T[Capacity];
}

Next we need to create the basic functions to perform the operations we require with a stack. The first operation needed is a way to add data to the stack. We do this using the Push method. Push will take an element of type T, and it will push it onto our stack. If we are out of space on our stack, Push will need to create more space for us, so we will also create a method for  increasing out capacity. To get data out of the stack we will need a Pop method. This method will remove the top element from the stack and return it to us. It should throw an error if the stack is empty. Sometimes it is needed in a stack to be able to check the top element without removing it. I will call this method peek. This could have been achieved by calling myStack.Push(myStack.Pop()), but I think it is much cleaner to just create this simple method.

public void Push(T element)
{
    if (this.Length == Capacity)
    {
        IncreaseCapacity();
    }
    Index++;
    Elements[Index] = element;
}
 
public T Pop()
{
    if (this.Length < 1)
    {
        throw new InvalidOperationException("Stack is empty");
    }
 
    T element = Elements[Index];
    Elements[Index] = default(T);
    Index--;
    return element;
}
 
public T Peek()
{
    if (this.Length < 1)
    {
        throw new InvalidOperationException("Stack is empty");
    }
 
    return Elements[Index];
}
 
private void IncreaseCapacity()
{
    Capacity++;
    Capacity *= 2;
    T[] newElements = new T[Capacity];
    Array.Copy(Elements, newElements, Elements.Length);
    Elements = newElements;
}

At this point we now have a working stack. We can push, pop, and peek. So now we write a tiny little command line program to test out our stack. I'll use some for loops to throw some data into the stack and use for loops to peek at the data and pop it off. I'll then make sure I can get my stack empty errors.

class Program
{
    static void Main(string[] args)
    {
        Stack<int> myStack = new Stack<int>();
        for (int i = 0; i < 50; i++)
        {
            Console.WriteLine("Pushing: " + i);
            myStack.Push(i);
            Console.WriteLine("New Length is: " + myStack.Length);
        }
 
        for (int i = 0; i < 50; i++)
        {
            Console.WriteLine("Peeking First: " + myStack.Peek());
            Console.WriteLine("Popping: " + myStack.Pop());
            Console.WriteLine("New Length is: " + myStack.Length);
        }
 
        try
        {
            myStack.Peek();
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine("As expected I received this error: " + ex.Message);
        }
 
        try
        {
            myStack.Pop();
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine("As expected I received this error: " + ex.Message);
        }
 
        Console.WriteLine("Press the enter key to exit");
        Console.ReadLine();
    }
}

As I said, this is not the most robust and best written stack ever. It isn't designed to be, but it explains the basics of how a stack works. I hope you've enjoyed this. Happy Generic Stack writing.