The Artima Developer Community
Sponsored Link

C# Answers Forum
what are generic types used for?

1 reply on 1 page. Most recent reply: Apr 24, 2008 9:36 PM by Chetan Panchal

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
raghav shukla

Posts: 1
Nickname: raghav16
Registered: Apr, 2008

what are generic types used for? Posted: Apr 21, 2008 12:26 PM
Reply to this message Reply
Advertisement
what are genric types used for?
are genric types only used for creating type safe collections. cant we just use them as simple type safe classes for exmaple can we use genric types in the following manner

consider the following scenario : in a particular electronics trading company they send order to suppliers. now the products they deal in are TV,REFERIGERATOR,WASHING MACHINE all provided by different suppliers. i.e. different orders are prepared for each Type of item.

the TV specification is defined by class TV. similarly REFERIGERATOR specifications are defined by class FRIDGE. and WASHING MACHINE by class WASHMACH. but the order for all the three has same details

1. item name
2. code
3. quantity
4. addItem()
5. editOrder()
6. SubmitOrder()

why cant we create a genric class named ORDER and keep passing type parameters for TV,FRIDGE,WASHMACH.

now here we are niether creating any linked list or binary tree or some kind of array list. i mean no collection is used. still using genric class beacuse it makes the work easier.

but is it necessary that generic types are just used for creating type safe collections?


Chetan Panchal

Posts: 1
Nickname: chetanpan
Registered: Apr, 2007

Re: what are generic types used for? Posted: Apr 24, 2008 9:36 PM
Reply to this message Reply
Generic type is used to create new parameterized type.

From your text, it is clear that you know value of, e.g. Generic List class, which can be later instantiated as List of Strings or ints or any other type. Note that all these 'lists' have common interface and that means, we can use polymorphisms to our advantage.

In you example, if you create Order<P, Q, R>, and later instantiate Order<TV,FRIDGE,WASHMACH>, you are limiting yourself. In this case, order can be of some TV,FRIDGE and/or WASHMACH only. What if, later (who knows future?), you want Mixer to be part of an order as well. Rather, it would make sense to create interface called IOrderItem and let a class implement it - so that it can be part of Order as Order will maintain maybe List<IOrderItem>.

Order composed of Items
Item must implement IOrderItem

Also, in your case, addItem() will accept which type of object? You may have to overload this method with parameter types of P, Q, R etc.

All in all, in your case, you are tight-coupling Order as compile time with TV, Fridge and WashMach classes. So, your Order class will be less reusable as you cannot add Mixer in your order! Order should be agnostic to real type of order item.

Generic class are supposed to be instantiated with different types as argument(s). If you are always going to instantiate Order class with TV, Fridge and WashMach class, there is no point in taking pain to define generic class at all!

I hope this helps.

Flat View: This topic has 1 reply on 1 page
Topic: loading a  dll Previous Topic   Next Topic Topic: Missing Build in C#

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use