none
Generic and their real use and advantage

    Question

  • Please some one show me the real advantage of using generic with few good examples. thanks
    Thursday, February 16, 2017 9:00 AM

Answers

  • Hi Mou_kolkata,

    Thank you for posting here.

    For the advantage of Generic, you could refer to the MSDN article Benefits of Generics (C# Programming Guide).

    Here is the simple example for reference. 

     class Program
        {
            static void Main(string[] args)
            {
                // Use the generic type Test with an int type parameter.
                Test<int> test1 = new Test<int>(5);
                // Call the Write method.
                test1.Write();
    
                // Use the generic type Test with a string type parameter.
                Test<string> test2 = new Test<string>("cat");
                test2.Write();
    
                Console.ReadKey();
            }
        }
        class Test<T>
        {
            T _value;
    
            public Test(T t)
            {
                // The field has the same type as the parameter.
                this._value = t;
            }
    
            public void Write()
            {
                Console.WriteLine(this._value);
            }
        }

    Here is the output.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Friday, February 17, 2017 8:56 AM
    Friday, February 17, 2017 7:44 AM
    Moderator
  • Mou_kolkata.

    Did you see my Tree example above? What more are you looking for?

    This is exactly the kind of data structure that would be used in real life.

    I know my example was only a rough idea, but it is easy to find a full proper implementation of a binary tree so I didn't see the need to repeat something that others have already documented much more extensively.

    See here, for example.

    I used something similar to this (admittedly not in C# but the idea applies) due to a requirement for users to define special conditions that data associated with other stored accounts had to meet.

    The users could define a set of basic requirements that they could name (they may define 3 requirements A, B and C for example). But they also needed to define complex requirements (stored in the db) that were boolean combinations of simple requirements. For example "(A AND B) OR (B AND C)".

    The best to way to process this to see if a record met the requirements was to parse the conditions and store these requirements as a set of binary trees where each node represented a boolean condition ("AND" or "OR") and each leaf represented a simple condition. Hence I needed a Binary Tree class.

    I could then have a process that evaluated the tree to acquire a final true or false value by walking the tree, testing the leaf conditions and combining them according to the nodes.

    There you go. Real Life.

    • Marked as answer by Mou_kolkata Wednesday, February 22, 2017 6:58 PM
    Friday, February 17, 2017 9:04 AM
  • Hi Mkolkata,

    Thank you for feedback.

    I am not sure what the real work example you want.

    Here is a simple example with simple use, constraints on Type Parameters, generic method.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace GenericExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                print p = new print(5);
                
                print1<int> p1 = new print1<int>(5);
                print1<string> p2 = new print1<string>("p2");
                //Constraints on Type Parameters
                print2<int, string> p3 = new print2<int, string>(5, "p3");
                //Generic method
                print3 p4 = new print3();
                p4.print("aaa", "bbb", "ccc");
                p4.print(111,222,333);
    
                Console.ReadLine();
            }
        }
        class print
        {
            public print(int i)
            {
                Console.WriteLine(i);
            }
        }
    
        class print1<T>
        {
            public print1(T t)
            {
                Console.WriteLine(t);
            }
        }
        class print2<T, V> where T : struct where V : class
        {
            public print2(T t, V v)
            {
                Console.WriteLine(t + " " + v);
            }
        }
    
        class print3
        {
            public print3() { }
            public void print<T>(params T[] p)
            {
                foreach (T t in p)
                {
                    Console.WriteLine(t);
                }
            }
        }
    }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, February 17, 2017 9:05 AM
    Moderator

All replies

  • It provides compile time safety, suppose you have a ArrayList (non generic collection) which you use in the code, but it is not type safe, as it can store an int,float,decimal  or whatever is passed:

    ArrayList nonGenericList = new ArrayList();
    
    nonGenericList.Add(1);
    nonGenericList.Add("ehsan");

    The problem here comes that when we want to get an item for example at index 1 we are not sure what type of item that will be so if we cast it to wrong type that would break and of-course when we add 1 to ArrayList it expects parameter of type Object and as every type inherits by default from Object in C#, so this will cause boxing to happen which converting this value type to reference type object which degrades performance. Now in above code if we want the integer out of the list i would have to do casting :

    int number = (int)nonGenericList[0];

    Now if we want to avoid boxing and unboxing we can use a generic List which we have in .NET framework now and it would cause compile time error if we try to add wrong type of object in it, see below example:

    List<int> integers = new List<int>();
    
    integers.Add(1);
    integers.Add("ehsan"); // this will cause compile time error as string is not integer

    Hope it help!


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites




    • Edited by Ehsan Sajjad Thursday, February 16, 2017 9:34 AM
    Thursday, February 16, 2017 9:33 AM
  • To know the advantage you must use C# without it (simply change the standard using Collections.Generic to Collections)

    Writing ten thousand times (Mou)x[index] cost beside the chance on errors much more time then writing x[index]

    Of course the casting is done behind the scene, but than in a much more optimized way. 


    Success
    Cor

    Thursday, February 16, 2017 10:00 AM
  • when some one would write generic class instead of regular class. if possible show me a good example. thanks
    Thursday, February 16, 2017 10:54 AM
  • The existing generic collections mentioned by the others above should give you an idea of exactly why you would want to use a generic class.

    But lets say you need a different data structure in your program - a (binary) Tree structure, which as far as I know doesn't exist in the .net libraries (something I needed myself for a simple 'equation parsing' routine once).

    Now you could simply define your tree classes to contain a specific datatype if you know that, in your program, you only need to hold strings, but that would not be very flexible and would prevent you from using a potentially useful piece of code in other situations.

    You could make your tree just hold objects, but this would not be as good for all the reasons the other posters described above (lack of type safety, overhead of casting, etc).

    So...you make your tree classes generic (code just a very rough concept off the top of my head):

    public class TreeNode<T>
    {
        private T  _dataItem;
    
        public TreeNode(T value)
        {
            _dataItem = value;
        }
    
        public T DataItem {get {return _dataItem;}}
    }
    
    
    public class Tree<T>
    {
        private TreeNode<T> leftNode;
        private TreeNode<T> rightNode;
    
        // etc
    }

    Thursday, February 16, 2017 11:17 AM
  • do you mean to say the boxing and unboxing still happens for generic collection?


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 16, 2017 11:18 AM
  • The type is given in the (under the skin) used arraylist, however AFAIK stays the rest optimized the same. 

    Be aware that boxing and unboxing in C# takes immeasurable time. It is in my perception overdone warned by persons coming from a very early stage of OOP programming. For me if that does not happen it is not an advantage; 1 micro second in a day in a very large process does seldom matter. 

    Try once to measure it, you will probably be surprised. 


    Success
    Cor

    Thursday, February 16, 2017 1:04 PM
  • Hi Mou_kolkata,

    Thank you for posting here.

    For the advantage of Generic, you could refer to the MSDN article Benefits of Generics (C# Programming Guide).

    Here is the simple example for reference. 

     class Program
        {
            static void Main(string[] args)
            {
                // Use the generic type Test with an int type parameter.
                Test<int> test1 = new Test<int>(5);
                // Call the Write method.
                test1.Write();
    
                // Use the generic type Test with a string type parameter.
                Test<string> test2 = new Test<string>("cat");
                test2.Write();
    
                Console.ReadKey();
            }
        }
        class Test<T>
        {
            T _value;
    
            public Test(T t)
            {
                // The field has the same type as the parameter.
                this._value = t;
            }
    
            public void Write()
            {
                Console.WriteLine(this._value);
            }
        }

    Here is the output.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Friday, February 17, 2017 8:56 AM
    Friday, February 17, 2017 7:44 AM
    Moderator
  • this is very old example. please come with one real work example of generic class. thanks
    Friday, February 17, 2017 8:53 AM
  • no i am not talking about boxing and unboxing.

    i am looking for a nice generic class example look like real life example but please do not post TreeNode<T> example. thanks

    Friday, February 17, 2017 8:55 AM
  • Mou_kolkata.

    Did you see my Tree example above? What more are you looking for?

    This is exactly the kind of data structure that would be used in real life.

    I know my example was only a rough idea, but it is easy to find a full proper implementation of a binary tree so I didn't see the need to repeat something that others have already documented much more extensively.

    See here, for example.

    I used something similar to this (admittedly not in C# but the idea applies) due to a requirement for users to define special conditions that data associated with other stored accounts had to meet.

    The users could define a set of basic requirements that they could name (they may define 3 requirements A, B and C for example). But they also needed to define complex requirements (stored in the db) that were boolean combinations of simple requirements. For example "(A AND B) OR (B AND C)".

    The best to way to process this to see if a record met the requirements was to parse the conditions and store these requirements as a set of binary trees where each node represented a boolean condition ("AND" or "OR") and each leaf represented a simple condition. Hence I needed a Binary Tree class.

    I could then have a process that evaluated the tree to acquire a final true or false value by walking the tree, testing the leaf conditions and combining them according to the nodes.

    There you go. Real Life.

    • Marked as answer by Mou_kolkata Wednesday, February 22, 2017 6:58 PM
    Friday, February 17, 2017 9:04 AM
  • Hi Mkolkata,

    Thank you for feedback.

    I am not sure what the real work example you want.

    Here is a simple example with simple use, constraints on Type Parameters, generic method.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace GenericExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                print p = new print(5);
                
                print1<int> p1 = new print1<int>(5);
                print1<string> p2 = new print1<string>("p2");
                //Constraints on Type Parameters
                print2<int, string> p3 = new print2<int, string>(5, "p3");
                //Generic method
                print3 p4 = new print3();
                p4.print("aaa", "bbb", "ccc");
                p4.print(111,222,333);
    
                Console.ReadLine();
            }
        }
        class print
        {
            public print(int i)
            {
                Console.WriteLine(i);
            }
        }
    
        class print1<T>
        {
            public print1(T t)
            {
                Console.WriteLine(t);
            }
        }
        class print2<T, V> where T : struct where V : class
        {
            public print2(T t, V v)
            {
                Console.WriteLine(t + " " + v);
            }
        }
    
        class print3
        {
            public print3() { }
            public void print<T>(params T[] p)
            {
                foreach (T t in p)
                {
                    Console.WriteLine(t);
                }
            }
        }
    }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, February 17, 2017 9:05 AM
    Moderator