Wednesday, August 29, 2018

Generic & it's Example For Beginners :
A small Notes On generics.

By   using generics We can create a class with placeholder which are used for type of class members like method , variable .

For Example:

class Program
    {
        static void Main(string[] args)
        {
            mygenerics<int> mint = new mygenerics<int>();
            Console.WriteLine(mint.add(5, 6));

            mygenerics<string> mstr = new mygenerics<string>();
            Console.WriteLine(mstr.add("Rk", "Ki"));

            mygenerics<float> mfloat = new mygenerics<float>();
            Console.WriteLine(mfloat.add(20,3));

            mygenerics<double> mdouble = new mygenerics<double>();
            Console.WriteLine(mdouble.add(20.5, 3.05));

            Console.ReadKey();
        }
    }

    class mygenerics<t>
    {
        public t add(t a, t b)
        {
            t c = (dynamic)a + (dynamic)b;
            return c;
        }
    }


No comments:

Post a Comment