Why do I use this IEnumerable can obtain data, but with the List type to get les

There are two types: Product and ShoppingCart, if ShoppingCart inherits the List<Product>, obtained by using the following code products.Count=0; and to inherit from the IEnumerable<Product> we can get the correct value
The following code:
The Product class:
public class Product {
    public int ProductID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Category { set; get; }
}

Inheritance of List<Product> ShoppingCart:
public class ShoppingCart : List<Product>
{
    public List<Product> Products { get; set; }
}

Inheritance of IEnumerable<Product> ShoppingCart:
public class ShoppingCart : IEnumerable<Product>
{

    public IEnumerable<Product> Products { get; set; }

    public IEnumerator<Product> GetEnumerator()
    {
        return Products.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

Following a method call:
   // create and populate ShoppingCart 
        List<Product> products1 = new ShoppingCart
        {
            Products = new List<Product> {
                new Product {Name = "Kayak", Category = "Watersports", Price = 275M},
                new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
                new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
                new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
            }
        };  //Here is products1.count=0, may I ask why
           
        IEnumerable<Product> products2 = new ShoppingCart
        {
            Products = new List<Product> {
                new Product {Name = "Kayak", Category = "Watersports", Price = 275M},
                new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
                new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
                new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
            }
        }; //Here can correctly obtain the value

Started by Paul at February 09, 2016 - 2:38 AM

//Here is products1.count=0, may I ask why
Because you don't have to products1 to add elements, you add to the
Products1.Products
products1.Products.Count = 1

Posted by Newman at February 19, 2016 - 3:32 AM

List<Product> products1 = new ShoppingCart();
products1.Add(new Product {Name = "Kayak", Category = "Watersports", Price = 275M});
...
Only in this way.

Posted by Newman at March 06, 2016 - 3:35 AM

List<Product> products1 = new ShoppingCart
{
Products //You're here to add, not products1

Posted by Orville at November 15, 2016 - 9:53 PM

public IEnumerator<Product> GetEnumerator()
{
return Products.GetEnumerator();
}

If you write
public IEnumerator<Product> GetEnumerator()
{
return this.GetEnumerator();
}
Then you are not.

You return the Products attribute of the GetEnumerator.

Posted by Newman at November 19, 2016 - 9:55 PM

The attribute products1. behind only Count, not products1.Products.Count.

Posted by Paul at November 20, 2016 - 10:36 PM

ShoppingCart.Products. Count is the 4, did not understand that has been converted to a List type is 0.

Posted by Paul at December 03, 2016 - 11:26 PM

(products1 as ShoppingCart).Count

Posted by Newman at December 19, 2016 - 12:01 AM

Or 0

Posted by Paul at December 27, 2016 - 12:53 AM

(products1 as ShoppingCart).Products.Count

Posted by Newman at December 30, 2016 - 12:55 AM

Give you around in.

I said you don't understand? You don't have to add products1, you add to the products1.Products. Of course, products1.Count=0

Posted by Newman at January 03, 2017 - 2:26 AM

The landlord himself confused, ShoppingCart and ShoppingCart.Products have Count, I use the new, Count and with the former, certainly wrong, don't need to inherit.

Posted by Avery at January 11, 2017 - 1:52 AM