stackoverflow1453

Programming&&Games&&Life

Archive for June, 2017

Generic Classes – 2

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

namespace _09_06_2017_b
{
    class Test<T>
    {
        T _value;

        public Test(T t)
        {
            this._value = t;
        }

        public void Write()
        {
            Console.WriteLine(this._value);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test<int> t1 = new Test<int>(5);
            t1.Write();

            Test<string> t2 = new Test<string>("Alo");
            t2.Write();

            Test<double> t3 = new Test<double>(4.5);
            t3.Write();

            Console.ReadKey();
        }
    }
}

Generic Methods – 1

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

namespace _09_06_2017_a
{
    class Program
    {
        public void ReverseAndPrint<T>(T[] arr)
        {
            Array.Reverse(arr);

            foreach (var item in arr)
            {
                Console.Write("{0}", item.ToString());
            }
            Console.WriteLine(" ");
        }

        static void Main(string[] args)
        {
            var intArray = new int[] { 2, 5, 7, 3, 32, 24, 1, 5, 66, 7 };
            var stringArray = new string[] { "A", "B", "C", "D", "E", "F", "G" };
            var doubleArray = new double[] { 23, 4, 56, 7, 8 };

            Program p = new Program();

            p.ReverseAndPrint<int>(intArray);
            p.ReverseAndPrint(intArray);
            p.ReverseAndPrint<string>(stringArray);
            p.ReverseAndPrint(stringArray);
            p.ReverseAndPrint<double>(doubleArray);
            p.ReverseAndPrint(doubleArray);

            Console.ReadKey();
        }
    }
}

Generic Constraints

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

namespace GenericConstraint
{

    class Person { }
    class Program
    {
        public void Test<T>()
            where T : class  //Generic Constraint
        {
            Console.WriteLine("Hello");
        }

        static void Main()
        {
            Program p = new Program();
            p.Test<Person>();
            Console.ReadKey();
        }
    }
}

Generic Collections

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

namespace GenericCollectionSample
{
    class Employee
    {
        public string Name;
        public int Age;
        public string Address;
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Index based generic collection
            List<int> listObj = new List<int>();
            listObj.Add(23);
            listObj.Add(444);
            //Displaying list value using index
            Console.WriteLine("List Second Value: {0}", listObj[1]);


            //Key based generic collection
            Dictionary<int, string> objDic = new Dictionary<int, string>();
            objDic.Add(23, "Alo");
            Console.WriteLine("Printout the 23th element in dictionary: {0} ", objDic[23]);

            //Priority based generic collection(Stack)
            Stack<int> s1 = new Stack<int>();
            s1.Push(4);
            s1.Push(5);
            s1.Push(2);

            foreach (var item in s1)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("1st value which is gotten from stack: {0}", s1.Pop());
            Console.WriteLine("2nd value which is gotten from stack: {0}", s1.Pop());

            //Priority based generic collection (Queue)
            Queue<int> q1 = new Queue<int>();
            q1.Enqueue(32);
            q1.Enqueue(45);
            q1.Enqueue(67);

            foreach (var item in q1)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("1st guy which leaves the queue 1st: {0}", q1.Dequeue());

            //Creating Employee Records
            Employee e1 = new Employee();
            e1.Name = "Baran";
            e1.Age = 23;
            e1.Address = "Turkiye";

            Employee e2 = new Employee();
            e2.Name = "Taran";
            e2.Age = 265;
            e2.Address = "Yugoslavya";

            //Create generic list with employee records
            List<Employee> le1 = new List<Employee>();
            le1.Add(e1);
            le1.Add(e2);

            foreach (var item in le1)
            {
                Console.WriteLine(item.Name + " " + item.Age + " " + item.Address);
            }

            Console.ReadKey();
        }
    }
}

gen

Generic Class – 1

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

namespace GenericComparator
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();

            bool result = p.Compare(3, 4);
            Console.WriteLine("result=" + result);

            ComparatorGeneric<int> a = new ComparatorGeneric<int>();
            Console.WriteLine(a.Compare(4, 4));

            ComparatorGeneric<string> s = new ComparatorGeneric<string>();
            Console.WriteLine(s.Compare("Ali", "Ali"));

            Console.ReadKey();
        }

        //Method Overloading
        bool Compare(int x, int y)
        {
            if (x.Equals(y))
            {
                return true;
            }
            else return false;
        }

        bool Compare(string x, string y)
        {
            if (x.Equals(y))
            {
                return true;
            }
            else return false;
        }
    }
    //Generic Class Implementation
    class ComparatorGeneric<T>
    {
        public bool Compare(T x, T y)
        {
            if (x.Equals(y))
            {
                return true;
            }

            else return false;
        }
    }
}

is, as operators

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

namespace _08_06_2017_a
{
    class Person
    {
        public int age = 25;
        public string Name = "Anonymous";

    }
    class Employee : Person { }

    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            Employee Mahmut = new Employee();
            Employee e;

            Person p;
            //Check if variable can be converted to type Person
            if (Mahmut is Person)
            {
                e = Mahmut;
                Console.WriteLine("{0}, {1}", e.age, e.Name);
            }

            p = Mahmut as Person; //p=(Person)Mahmut

            if (p != null)
            {
                Console.WriteLine("{0}, {1}", p.age, p.Name);
            }
            Console.ReadKey();
        }
    }
}

User defined conversion

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

namespace _07_06_2017_d
{
    class Person
    {
        public string Name;
        public int Age;

        public Person(string n, int a)
        {
            Name = n;
            Age = a;
        }

        public static implicit operator int(Person p)
        {
            return p.Age;
        }

        public static implicit operator Person(int k)
        {
            return new Person("Anonimus", k);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person("Ahmet", 2);
            int z = p1;     //int z=int(Person p1)
            Console.WriteLine(z);

            int m = 35;

            Person p2 = m;   //Person p2= Person(35)

            Console.WriteLine(p2.Name + " " + p2.Age);
            Console.ReadKey();
        }
    }
}