Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generic Collection in C#

Generic Collection in C#

Scheduled Pinned Locked Moved C#
11 Posts 7 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ashwani Dhiman
    wrote on last edited by
    #1

    What is Generic Collection? Why we use Generic Collection?

    T K L 3 Replies Last reply
    0
    • A Ashwani Dhiman

      What is Generic Collection? Why we use Generic Collection?

      T Offline
      T Offline
      tom572007
      wrote on last edited by
      #2

      Generics are like lists. You can store some defined typed data, and then make a lot of operations like a sort, find an item, remove / add items, extract all items, etc...

      1 Reply Last reply
      0
      • A Ashwani Dhiman

        What is Generic Collection? Why we use Generic Collection?

        K Offline
        K Offline
        King Julien
        wrote on last edited by
        #3

        Are you faking some interview????? Why dont you search it in the google...????? Is it that hard to find???? There are million sites availing free tutorials on these topics.....

        Have a Happy Coding.....

        1 Reply Last reply
        0
        • A Ashwani Dhiman

          What is Generic Collection? Why we use Generic Collection?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Generic Collections (Array Lists) existed before non Generic (i.e. Lists). Obvously if you are storing a collection of one type of object a List is better suited to avoid casting. However you may have a situation where you want to maintain a collection of objects that are not necessarily the same object. They may have similar OO concepts (otherwise I have no idea why you would group them), but their actual TypeOf is different. In this case you would need to use the ArrayList to store the generic object. Again not sure why you would do this. The only time I am using ArrayList is when I have to go back to VS2003.

          C 1 Reply Last reply
          0
          • L Lost User

            Generic Collections (Array Lists) existed before non Generic (i.e. Lists). Obvously if you are storing a collection of one type of object a List is better suited to avoid casting. However you may have a situation where you want to maintain a collection of objects that are not necessarily the same object. They may have similar OO concepts (otherwise I have no idea why you would group them), but their actual TypeOf is different. In this case you would need to use the ArrayList to store the generic object. Again not sure why you would do this. The only time I am using ArrayList is when I have to go back to VS2003.

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Collin Jasnoch wrote:

            Generic Collections (Array Lists) existed before non Generic (i.e. Lists).

            You seem to have overloaded the word "generic" The ArrayList is not a Generic object in the OO sense of the word. To remove the ambiguity you may like to call them "general" rather than generic.

            Collin Jasnoch wrote:

            However you may have a situation where you want to maintain a collection of objects that are not necessarily the same object.

            Did you mean "same _type of object". I've never found a situation where I would repeatedly store the same object in a list.

            *Developer Day Scotland - Free community conference Delegate Registration Open

            _

            L 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Collin Jasnoch wrote:

              Generic Collections (Array Lists) existed before non Generic (i.e. Lists).

              You seem to have overloaded the word "generic" The ArrayList is not a Generic object in the OO sense of the word. To remove the ambiguity you may like to call them "general" rather than generic.

              Collin Jasnoch wrote:

              However you may have a situation where you want to maintain a collection of objects that are not necessarily the same object.

              Did you mean "same _type of object". I've never found a situation where I would repeatedly store the same object in a list.

              *Developer Day Scotland - Free community conference Delegate Registration Open

              _

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Colin Angus Mackay wrote:

              You seem to have overloaded the word "generic" The ArrayList is not a Generic object in the OO sense of the word. To remove the ambiguity you may like to call them "general" rather than generic

              The arraylist is of type object. I used the word Generic because that is the word that was used in the question and I am assuming the question is about ArrayList. The word generic or general is irrelevant as long as my assumption is correct (stick with that same wording as the questionaire).

              Colin Angus Mackay wrote:

              Did you mean "same type of object". I've never found a situation where I would repeatedly store the same object in a list.

              Yes I meant same object type (i.e. objects that are not of the same type stored in a collection). FYI you could have a situation where you want to store the same specific object in a list. The list does not keep copies but actual references. This is how you can keep ojects alive so the GC does not hit them. How you determine when to add and remove from the list would depend on what you are doing and what the data is. However, this was not what I was refering to.

              S C 2 Replies Last reply
              0
              • L Lost User

                Colin Angus Mackay wrote:

                You seem to have overloaded the word "generic" The ArrayList is not a Generic object in the OO sense of the word. To remove the ambiguity you may like to call them "general" rather than generic

                The arraylist is of type object. I used the word Generic because that is the word that was used in the question and I am assuming the question is about ArrayList. The word generic or general is irrelevant as long as my assumption is correct (stick with that same wording as the questionaire).

                Colin Angus Mackay wrote:

                Did you mean "same type of object". I've never found a situation where I would repeatedly store the same object in a list.

                Yes I meant same object type (i.e. objects that are not of the same type stored in a collection). FYI you could have a situation where you want to store the same specific object in a list. The list does not keep copies but actual references. This is how you can keep ojects alive so the GC does not hit them. How you determine when to add and remove from the list would depend on what you are doing and what the data is. However, this was not what I was refering to.

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                "Generic Collections (Array Lists) existed before non Generic (i.e. Lists)" Actually, lists (List <T>) are generic and array lists are non-generic. You either got them mixed up, Or you're using the term "generic" to mean "any type". In any case, .NET uses the term to mean parameterized types.

                Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                L 1 Reply Last reply
                0
                • L Lost User

                  Colin Angus Mackay wrote:

                  You seem to have overloaded the word "generic" The ArrayList is not a Generic object in the OO sense of the word. To remove the ambiguity you may like to call them "general" rather than generic

                  The arraylist is of type object. I used the word Generic because that is the word that was used in the question and I am assuming the question is about ArrayList. The word generic or general is irrelevant as long as my assumption is correct (stick with that same wording as the questionaire).

                  Colin Angus Mackay wrote:

                  Did you mean "same type of object". I've never found a situation where I would repeatedly store the same object in a list.

                  Yes I meant same object type (i.e. objects that are not of the same type stored in a collection). FYI you could have a situation where you want to store the same specific object in a list. The list does not keep copies but actual references. This is how you can keep ojects alive so the GC does not hit them. How you determine when to add and remove from the list would depend on what you are doing and what the data is. However, this was not what I was refering to.

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  Collin Jasnoch wrote:

                  The word generic or general is irrelevant

                  That is like saying "the word left or right is irrelevant". It isn't irrelevant. Generic has a very specific and well defined meaning in OO languages. If the OP used it in error then don't repeat the error. Use the correct term otherwise the OP will continue to use the word in error and cause more confusion later.

                  Collin Jasnoch wrote:

                  FYI you could have a situation where you want to store the same specific object in a list. The list does not keep copies but actual references.

                  Are you trying to teach me C#? I am well aware of this. I just said I've never found a situation where I would want to do this.

                  Collin Jasnoch wrote:

                  This is how you can keep ojects alive so the GC does not hit them.

                  Adding an object many times to the same list will not affect the way garbage collection works. Just one reference in the list is sufficient for the GC to ignore the object so long as the list itself is referenced.

                  *Developer Day Scotland - Free community conference Delegate Registration Open

                  L J 2 Replies Last reply
                  0
                  • C Colin Angus Mackay

                    Collin Jasnoch wrote:

                    The word generic or general is irrelevant

                    That is like saying "the word left or right is irrelevant". It isn't irrelevant. Generic has a very specific and well defined meaning in OO languages. If the OP used it in error then don't repeat the error. Use the correct term otherwise the OP will continue to use the word in error and cause more confusion later.

                    Collin Jasnoch wrote:

                    FYI you could have a situation where you want to store the same specific object in a list. The list does not keep copies but actual references.

                    Are you trying to teach me C#? I am well aware of this. I just said I've never found a situation where I would want to do this.

                    Collin Jasnoch wrote:

                    This is how you can keep ojects alive so the GC does not hit them.

                    Adding an object many times to the same list will not affect the way garbage collection works. Just one reference in the list is sufficient for the GC to ignore the object so long as the list itself is referenced.

                    *Developer Day Scotland - Free community conference Delegate Registration Open

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Colin Angus Mackay wrote:

                    Are you trying to teach me C#? I am well aware of this. I just said I've never found a situation where I would want to do this.

                    Not trying to teach you. Just explaining why you would do this. You made it sound like it would be totally pointless. See bellow.

                    Colin Angus Mackay wrote:

                    Adding an object many times to the same list will not affect the way garbage collection works. Just one reference in the list is sufficient for the GC to ignore the object so long as the list itself is referenced.

                    It does when your algorithm removes from the list based on a lack of need for that object. i.e. when the need has diminished so have all references.

                    1 Reply Last reply
                    0
                    • S S Senthil Kumar

                      "Generic Collections (Array Lists) existed before non Generic (i.e. Lists)" Actually, lists (List <T>) are generic and array lists are non-generic. You either got them mixed up, Or you're using the term "generic" to mean "any type". In any case, .NET uses the term to mean parameterized types.

                      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Yes I messed up the wording. Lists are generic. In their usage they end up specific.(i.e. int, float, MyObj) Where as an ArrayList is always any type (object) and must be casting if it is to be used otherwise.

                      1 Reply Last reply
                      0
                      • C Colin Angus Mackay

                        Collin Jasnoch wrote:

                        The word generic or general is irrelevant

                        That is like saying "the word left or right is irrelevant". It isn't irrelevant. Generic has a very specific and well defined meaning in OO languages. If the OP used it in error then don't repeat the error. Use the correct term otherwise the OP will continue to use the word in error and cause more confusion later.

                        Collin Jasnoch wrote:

                        FYI you could have a situation where you want to store the same specific object in a list. The list does not keep copies but actual references.

                        Are you trying to teach me C#? I am well aware of this. I just said I've never found a situation where I would want to do this.

                        Collin Jasnoch wrote:

                        This is how you can keep ojects alive so the GC does not hit them.

                        Adding an object many times to the same list will not affect the way garbage collection works. Just one reference in the list is sufficient for the GC to ignore the object so long as the list itself is referenced.

                        *Developer Day Scotland - Free community conference Delegate Registration Open

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #11

                        Colin Angus Mackay wrote:

                        Generic has a very specific and well defined meaning in OO languages.

                        I doubt that. It is rather difficult even to find an authoritative definition of "object oriented language". But if you do have an authoritative definition then I would for one would like to see it.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups