Saturday, October 5, 2013

Generic Class (Custom Generic Class) Creatoin

    class clsMylist<T>
    {
        T[] arr;
        int size;
        int count;

        //Creating Public proeprty
        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        //Constructer of the same class
        //Is like a method but no return type
        public clsMylist()
        {
            //Access the property
            //assign value to property
            size = 5;
            arr = new T[size];
        }


        //Method in Class
        public void Add(T data)
        {
            arr[count] = data;
            count++;
        }

        //Defining the Indexer for T(Because we dont knwoe the dataType of T)
        public T this[int x]
        {
            get
            {
                //beacuse we are storing the list in array so that
                //X is the index for list
                return arr[x];
            }
            set
            {
                arr[x] = value;
            }
        }

    }

    class TestMylist
    {
        static void Main()
        {
            clsMylist<int> nums = new clsMylist<int>();
            nums.Add(10);
            nums.Add(20);
            nums.Add(30);
            nums.Add(40);
            nums.Add(50);

            /* Console.WriteLine(nums[2]);

           for (int i = 0; i < nums.Count-1; i++)
           {
               Console.WriteLine(nums[i]);
           }
           //Indexer Concept
   
            
            
            */

            clsMylist<Employee> emps = new clsMylist<Employee>();
            emps.Add(new Employee { EmpNo = 1010, EmpName = "Venu", Dept = "HR", Salary = 12000 });
            emps.Add(new Employee { EmpNo = 1010, EmpName = "Venu", Dept = "SWE", Salary = 12000 });
            emps.Add(new Employee { EmpNo = 1011, EmpName = "srinu", Dept = "CISF", Salary = 13000 });
            emps.Add(new Employee { EmpNo = 1012, EmpName = "Shekar", Dept = "BSF", Salary = 14000 });
            emps.Add(new Employee { EmpNo = 1013, EmpName = "Umakar", Dept = "Student", Salary = 15000 });

            /*  for (int i = 0; i < emps.Count; i++)
              {
               //Not require IEnumerator<> collection
              } */

            //foreach statement cannot operate on variables of type 'Demos.clsMylist<Demos.Employee>' because 'Demos.clsMylist<Demos.Employee>' does not contain a public definition for 'GetEnumerator'       C:\C#Advanced\Demos\Demos\clsMylist.cs   93     13     Demos

            /*  foreach (Employee emp in emps)
              {
                  Console.WriteLine(emp);
              }*/
            Console.ReadLine();

        }

    }

    class Employee
    {
        public int EmpNo { get; set; }
        public string EmpName { get; set; }
        public string Dept { get; set; }
        public int Salary { get; set; }

        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3}", EmpNo, EmpName, Dept, Salary);
        }

    }

    class clsMyCalender
    {
        string[] months = { "Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec", };
        string[] Weeks = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", };
    }


No comments: