Friday, January 16, 2015

Generic Class


//MyLists.cs
//--------------



namespace Generic_Class
{
    public class MyList<T>
    {
        T[] array;
        int count;
        int index;

        public MyList(int capacity)
        {
        array = new T[capacity];
        count = 0;
        index = -1;
        }

        public int Count
        { get { return count; } }
        public int Capacity
        { get { return array.Length; } }

        public void Add(T item)
        {
            if (index >= array.Length - 1)
            {
                //resize ... create a new one and copy old to new
                T[] temArray = new T[array.Length * 2];

                Array.Copy(array, temArray, array.Length);
                array = temArray;
            }
            index++;
            array[index] = item;
            count++;
        }

        public IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i <= index; i++)
            {
                yield return array[i]; //return back and forth
            }
        }
    }
}
//Exercise:
//1. Write a method that takes an array of ints and two indices i, j
//the method is to swap the items at i and j
//2. Turn this method into a generic method that accepts an array of any type

//=====================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Generic_Class
{
    //namespace
    public delegate T MyDelegate<T>(T x, T y);
    ///define a generic delegate that takes a value of any type and return a boolean
    public delegate bool Mypredicate<T>(T x);
    ///Define a generic delegate that takes a  value of some type and return a value of another type
    public delegate U MyDelegate<T, U>(T x);
    public delegate Tresult MyFunction<T, Tresult>(T x);
   
    public partial class Form1 : Form
    {
        MyList<int> list = new MyList<int>(8);
        MyList<string> slist = new MyList<string>(8);

        MyDelegate<float> floatdelegate;
        MyDelegate<double> doubledelegate;
        MyDelegate<uint> uinDelegate;
        MyDelegate<long> longDelegate;

        Mypredicate<int> intpredicate;

        MyFunction<int[], double> function1;//for named method
        MyFunction<int[], double> function2;//for lambda expression
        MyFunction<int[], double> function3;//method in lambda expression

        public Form1()
        {
            InitializeComponent();
            list.Add(9);
            list.Add(23);
            list.Add(2);
            list.Add(-12);
            list.Add(-6);

            slist.Add("Monday");
            slist.Add("Tuesday");
            slist.Add("Wednesday");

            DisplayList<int>(list);
            DisplayList<string>(slist);

            //named method
            floatdelegate = Add;//this is the Add that takes 2 floats
            doubledelegate = Add;//this is the add that takes 2 doubles
            //anonymous method
            uinDelegate = delegate(uint a, uint b)//this is very use....we now use the lambda expression
            {
                return a * b;
            };
            //lambda expression
            longDelegate = (a, b) => a - b;
            ////////////////////////////////////////////////////////////////////
            //lambda expression
            intpredicate = a => a < 0 && a % 2 == 0;
            //we will see a lot of delegates
            ///////////////////////////////////////////////////////
            function1 = Average;//one way

            function2 = array =>//another way
            {
                double sum = 0;
                foreach (int num in array)
                    sum += num;
                return sum / array.Length;
            };
            //you could still use method with lambda exprssion
            int[] array1 = { 3, 4, 5, 5 };
            function3 = (a) => Average(a);
        }

        private void DisplayList<T>(MyList<T> list)
        {
            foreach(T item in list)
            {
                richTextBox1.AppendText(item + "\n");
            }
        }

        //Exercise:
        //1. Write a method that takes an array of ints and two indices i, j
        //the method is to swap the items at i and j

        public void Swap(int[] a, int i, int j)
        {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
        //2. Turn this method into a generic method that accepts an array of any type
        public void Swap<T>(T[] a, int i, int j)
        {
            T temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
        //=====================================
        private float Add(float a, float b)
        {
            return a + b;
        }
        private double Add(double a, double b)
        {
            return a + b;
        }
        //method that takes an array of int and returns the average (double)
        private double Average(int[] array)
        {
            double sum = 0;
            foreach (int num in array)
                sum += num;
            return sum / array.Length;
        }
    }
}
//Lab Exercise: 
//Define a generic delegate that takes 2 values and return a value 
//all of the same type
//Define a generic delegate that takes a value of any type and 
//return a boolean
//Define a generic delegate that takes a value of some type and
//return a value of another type.

No comments:

Post a Comment