Hi friends,

My requirement is to maintain a collection of products based upon categories and i have to retrieve that.For ex:i can retrieve different book names which are under the category Book..Say i can retrieve all mobile company names which are under the category Mobile.I was told to use collections here... What i suppose to do is to use list of arraylists...The list contains the category(Book) and the arraylist under that list contains the products(All book names under book)

  • ------>[Array lists under the list]

  • [Book]----->[TamilBook][EnglisgBook][MathsBook]

  • [Mobiles]--->[Nokia][SonyEricson][Motorola]
  • I need code in C#.Net...Any known friends help me...

  • Thanks in advance....sherin

Dani AI

Generated

Good direction already from and : avoid loose, untyped collections and think about a category-to-products mapping rather than a single monolithic ArrayList. Below are practical design notes and implementation concerns that make the collection both safe and easy to work with long term.

If a product is just a name, a simple per-category container is fine. If products have attributes (id, sku, price, description), model them as objects and keep either:

  • a Category object that owns a collection of its products, or
  • a single master product list plus a fast index that maps category keys to product lists (this is useful when items can be in many categories or when you expect many products).

Retrieval patterns: for occasional queries you can group on demand (e.g., using LINQ GroupBy over a flat list). For frequent reads prefer a pre-built index (map) for O(1) lookups. Use an integer id for the category key where possible to avoid typos; if you must use names, make keys case-insensitive. Keep the index and the master list in sync: always update both when adding/removing products or rebuild the index after batch changes.

Concurrency, UI, and persistence notes: use thread-safe collections or a ConcurrentDictionary when multiple threads will mutate the collections. For data binding in UI apps, expose observable collections and implement change notification. Persist canonical data in a database (normalized Category and Product tables) or in JSON/XML; load into in-memory structures at startup and rebuild any runtime indexes after deserialization.

Troubleshooting tips: return empty sequences instead of nulls when a category is missing; validate keys before lookup; guard against duplicate product ids; and log index rebuilds if you see stale data. These small practices make a simple category->products solution robust and maintainable as the app grows.

Recommended Answers

All 2 Replies

Don't use ArrayList, for your example; better to use generics, as you know what datatypes to store.

Class Category {}
Class Book : Category{}
Class Mobile : Category{}

List<Book> books = new List<Book>();
List<Mobile> mobiles = new List<Mobile>();

List<Category> cat = new List<Book>();

Yeah, if you know what you're storing, use generics. You could have your top level actually be a Dictionary<string,List<BaseObject>>. the string part would act as the index (like "books," for example) so you could look it up later in the collection, then the list of BaseObjects could be the actual entries. If the list is really just strings then you'd use: Dictionary<string, List<string>> That way you don't have to muck around with Polymorphism.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.