using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SimplestGenericCS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // Create a standard ArrayList

            ArrayList al = new ArrayList();

            // Create an integer, store it in the ArrayList
            // Boxing takes place

            int i = 5; 
            al.Add(i);

            // Create a color, store it in the ArrayList
            // The weak typing of the ArrayList allows this,
            // but it is most likely a logic error

            Color c = Color.AliceBlue;
            al.Add(c);

            // Fetch the int from the ArrayList.
            // Cast is required, one more source of possible error
            // Unboxing takes place

            int j = (int)al[0];

        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Create a new generic List, telling it to accept only 
            // objects of class int

            System.Collections.Generic.List<int> l = 
                new System.Collections.Generic.List<int>();

            // Create a new int, add it to the list.
            // Boxing does NOT take place

            int i = 7;
            l.Add(i);


            // Create a new color

            Color c = Color.AntiqueWhite;

            // Attempting to add the color to the list that accepts only Points
            // causes a compiler error. This line does NOT work.

            //l.Add(c);

            // Fetch the int from the ArrayList
            // Cast is not needed
            // Boxing does not take place

            int j = l[0];

        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Create one of my GenericHolder objects that knows to
            // hold doubles. Place a double in it.

            GenericHolder<double> ghd = new GenericHolder<double>();
            ghd.Add(1.0);


            // Create one of my GenericHolder objects that knows to
            // hold ints. Place an int in it.

            GenericHolder<int> ghi = new GenericHolder<int>();
            ghi.Add(2);

            GenericHolder<MyOwnClass> ghmoc = new GenericHolder<MyOwnClass>();

            ghmoc.Add (new MyOwnClass()) ;

        }

        private Nullable<int> ni ;

        private void button4_Click(object sender, EventArgs e)
        {
            if (ni == null)
            {
                MessageBox.Show("ni is null");
            }
            else
            {
                MessageBox.Show("ni value = " + ni.ToString());
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ni = Convert.ToInt32(textBox1.Text);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ni = null;
        }
    }
}