Share via


c# How to get items from List

Question

Monday, January 22, 2018 4:43 AM

I created 2 Classes: Fruit and Vegetable. I created two Fruit and one Vegetable object and added them to a List. I want to write to the Console each object created, ie. Apple, Banana, Carrot. I can only get it to write that it created 2 Fruit and 1 Vegetable object. Here is the code.

using System;
using System.Collections.Generic;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var produce = new List<object>();

            var f1 = new Fruit
            {
                Name = "Apple",
                Weight = 6.0,
                Quantity = 2
            };
            produce.Add(f1);

            var f2 = new Fruit
            {
                Name = "Banana",
                Weight = 4.1,
                Quantity = 5
            };
            produce.Add(f2);

            produce.Add(new Vegetable());
            ((Vegetable)produce[2]).Name = "Carrot";
            ((Vegetable)produce[2]).Weight = 4.0;
            ((Vegetable)produce[2]).Quantity = 11;            
                     
            foreach(var item in produce)
            {
                Console.WriteLine(item); // this only shows if it is a Fruit or Vegetable
            }          
        }
    }
    class Fruit
    {
        private string name;
        public string Name
        {
            get { return name.ToUpper(); }
            set { name = value; }
        }
        public double Weight { get; set; }
       
        public int Quantity { get; set; }
    }
    class Vegetable
    {        
        public string Name { get; set; }        
       
        public double Weight { get; set; }
       
        public int Quantity { get; set; }
    }
   
}

This is the output:

I want to show all the List items and their properties: Name, Weight, Quantity. How would I do that?

All replies (3)

Monday, January 22, 2018 6:06 AM âś…Answered | 3 votes

Check the next solution too:

class Fruit

{

   . . .

   public override string ToString()

   {

      return string.Format( "Fruit: {0}, weight: {1}, quantity: {2}", Name, Weight, Quantity );

   }

}

 

class Vegetable

{

   . . .

   public override string ToString()

   {

      return string.Format( "Vegetable: {0}, weight: {1}, quantity: {2}", Name, Weight, Quantity );

   }

}

 

Since many things are similar, this leads to the idea of having a common base class and several derived classes.


Monday, January 22, 2018 5:13 AM

Your instructor probably taught you the difference between a generic and a non-generic collection; he or she should have taught that. Generic collections are good because they are type-safe; you can only use the collection for the type (class) that is specified. Your program is using a generic collection but you are adding different types, both the Fruit and the Vegetable types. Your program is adding both types because you specify the type is "object". That is not good programming and I hope your instructor did not teach you to do that. You can do it for now in this program but please understand that if you continue programming that way most other programmers will not like it.

If you want to keep the program like that and use objects for the items of the "List" (as in List<object>) then when you get an item out you need to cast each object back to the type that it was originally. One big problem is, how does your program know what to cast each item to? Do you understand that problem? If you don't understand and someone gives you the answer for how to do it then it is likely you won't understand how to do it the next time.

Sam Hobbs
SimpleSamples.Info


Monday, January 22, 2018 1:41 PM

First of all, I'm not a student. I'm 49 years old teaching myself to program.

Unfortunately all the googling I did only shows how to extract from a List<string> which doesn't work for an object. Maybe I am doing it wrong and will have to find some documentation on the difference between generic collections.