List of two different Types in C#

I'm currently having a problem with a ShoppingCart for my customer.

He wants to be able to add Text between the CartItems so I was wondering if there is some way to still only have one List.

My solution would be to have two lists, one of type IList that gets iterated over when calculating Weight and overall Price of the Cart while having another IList that only exposes the necessary fields for displaying it in the ListView and that is a SuperType of CartItem. (But how do I then access additional fields for the listView, defaulting weight and price to 0 in the Description-Text-Class would break LSP).

But having two lists somehow feels a bit odd (and still gives me problems), so I was wondering if I could do some sort of a TypedList where I specify the Type of each item.

Any suggestions are welcome, I'm not really happy with both options.


Use an interface:


And make your list be:


Now, create several types, have all of them implement this interface, and you can store them all safely in your list.

Alternatively, if you want there to be some default logic in a CartItem, use a base class instead of an interface.

You can make a class and, inside of that, define the properties of the required list type and then make a list of same class.

For example, if I wanted to make a list of strings and bools, I would make two properties in one class and then make a list of that class.

The Interface sounds like overkill. I'd just add a property to your current CartItem named something like "TextAfterItem".

Also: make sure your customer understands the cost of this feature in terms of security overhead. It sounds like they think this should be a simple update, but you're allowing users to enter text that will be displayed directly back to the page, and that's a dangerous proposition.

Need Your Help

Radio buttons in SSRS Report

reporting-services report radio-button

How do I add radio buttons as my parameter type in SSRS reports?

How to set openmp thread stack to unlimited?

fortran openmp intel-fortran stack-size

Can someone tell me how to set OpenMP stack size to unlimited?