Saturday, October 5, 2013

Multiple Polymorphisim

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    // step 1 (1)
    interface InterfaceA
    {
        int add(int a, int b);
        int Multi(int x, int y);
    }

    //Step 1(b)
    interface InterfaceB
    {
        int add(int a, int b);
        int Sub(int x, int y);
    }

    //Step 2
    class MultiInterfaces : InterfaceA, InterfaceB
    {

        int InterfaceA.add(int a, int b)
        {
            return a + b;
        }

        int InterfaceA.Multi(int x, int y)
        {
            return x * y;
        }

        int InterfaceB.add(int a, int b)
        {
            return a + b + 200;
        }

        int InterfaceB.Sub(int x, int y)
        {
            return x - y;
        }
    }

    //Step 3
    //Calling the methods from the inherited classf from step 2
    class ClsTestInterfaces : MultiInterfaces
    {
        static void Main()
        {

            //MultiInterfaces objMulIntf = new MultiInterfaces();
          
             //Runtime polymorphism
            InterfaceA ia = new MultiInterfaces();
            InterfaceB ib = new MultiInterfaces();

            Console.WriteLine("{0}",ia.add(10,20));
            Console.WriteLine("{0}", ia.Multi(10, 20));
            Console.WriteLine("{0}", ib.add(10, 20));
            Console.WriteLine("{0}", ib.Sub(10, 20));

            Console.ReadLine();

        }
    }

}

Generics Use Extension Methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    //For creating a static method class shoud be static
    public static class clsUserExtenstionMethod
    {
        //the first parameter type will be associated with "this" keyword
        //Ex:this string s
        //For string object it will get the intellisence
        public static int MyWordCount(this string s)
        {
            return s.Split(' ').Count();
        }

        //
        public static int CalcBonus(this Employee e, int rate)
        {
            return e.Salary * rate / 100;
        }
    }

    class TestUserExtentionMethod
    {

        static void Main()
        {
            CallingExtentionMethod();
            CalCulatingBonus();
        }


        public static void CallingExtentionMethod()
        {
            string msg = "test";
            int x = msg.MyWordCount();
        }      

        private static void CalCulatingBonus()
        {
            foreach (Employee emp in EmployeeList())
            {
                //Calling the User Custom Method
                Console.WriteLine("Bonus {0}", emp.CalcBonus(30));
            }
        }
       
        private static List<Employee> EmployeeList()
        {
            List<Employee> empList = new List<Employee> { new Employee { EmpNo = 1013, EmpName = "Umakar", Dept = "Student", Salary = 15000 },
                                                          new Employee { EmpNo = 1010, EmpName = "Venu", Dept = "HR", Salary = 12000 } };
            return empList;
        }
    }



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

}


Generic List anonymous types in c# example

Anonymus Types:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    class clsAnonymasTypes
    {
        static void Main()
        {
            var person = new { name = "Venu gopal", age = 27 };

            /* // Basic creation of Anonymus type
           
            Console.WriteLine("{0},{1}",person.name,person.age);
            Console.ReadLine();
           */

           // step 2
            //Instantiate the Employee list

            // List<Employee> empList = new List<Employee>();

            List<Employee> empList = new List<Employee> { new Employee { EmpNo = 1013, EmpName = "Umakar", Dept = "Student", Salary = 15000 },
                                                          new Employee { EmpNo = 1010, EmpName = "Venu", Dept = "HR", Salary = 12000 } };


            empList.Add(new Employee { EmpNo = 1010, EmpName = "Venu", Dept = "SWE", Salary = 12000 });
            empList.Add(new Employee { EmpNo = 1011, EmpName = "srinu", Dept = "CISF", Salary = 13000 });
            empList.Add(new Employee { EmpNo = 1012, EmpName = "Shekar", Dept = "BSF", Salary = 14000 });

            //step 3
            //Var can store all type of data
            //In Case 1:  it wil store the object(Containing all type "int,string,float" etc)
            //In Case 2: it will stoire the data of type "string" Only
            //In Case 3: It will filter and store only two columns of type "string" and "Int"
            var emps = from e in empList
                       //case 1: we can select all column by writing" select e"
                       // case 2: if we want to select only Name from list, we write as  "select e.EmpName"
                       // case 3: if We want to  slect a specified columns from the list, we write as "select new{ column1,column 2}"
                       //we can chage the column naes also as per requirement
                       select new { EmployeeName = e.EmpName, EmployeeSalary = e.Salary }
                          ;

          //Using Lamda Expression
            var emp1 = empList.Select(e => new { e.EmpName, e.Salary });

           //To get  a sum  of salaries

            var sum = empList.Sum(e => e.Salary);

            //Step 4
            foreach (var data in emps)
            {
                Console.WriteLine("{0},{1}", data.EmployeeName, data.EmployeeSalary);
            }

            Console.ReadLine();

        }

    }

    class Employee
    {
        //Step 1(a)
        public int EmpNo { get; set; }
        public string EmpName { get; set; }
        public string Dept { get; set; }
        public int Salary { get; set; }


        //Step 1(b)
        //Wew can OverRide the Default methods as per requirement
        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3}", EmpNo, EmpName, Dept, Salary);
        }
    }
}



I Emumabrable Example

I Emumabrable:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Demos
{
    //In Heric the List Collection from IEnumerable<T> Collection
    //Implemt the Interfaceto  the IEnumerator<T> GetEnumerator()
    class clsMylist<T> : IEnumerable<T>
    {
        T[] arr;
        int size;
        int count;

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

        //2
        //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];
        }

        //3
        //Method in Class
        public void Add(T data)
        {
            arr[count] = data;
            count++;
        }
       
        //step 5(a)
        //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;
            }
        }


        //Step 6
        public IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i < arr.Length; i++)
            {
                yield return arr[i];
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

    class TestMylist
    {
        static void Main()
        {

            //Step 4
            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
            
            */
            //Step 5(c)
            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
              } */


            //Step 7(b)
            //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'
            //For the above E
            foreach (Employee emp in emps)
            {
                Console.WriteLine("for IEmumarable   " + emp);
            }
            Console.ReadLine();

        }

    }


    class Employee
    {

        //Step 5(b)
        public int EmpNo { get; set; }
        public string EmpName { get; set; }
        public string Dept { get; set; }
        public int Salary { get; set; }


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



}

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", };
    }