safaribook-pro-aspnet-mvc3/chap5_essential_language_fe.../essential_csharp_features/Program.cs

43 lines
1.2 KiB
C#
Raw Normal View History

using System;
2021-07-05 18:21:52 +10:00
using System.Collections.Generic;
using System.Diagnostics;
2021-07-05 18:21:52 +10:00
using automatically_implemented_properties;
namespace automatically_implemented_properties
{
class Program
{
static void Main(string[] args)
{
2021-07-05 18:21:52 +10:00
// create and populate Shopping Cart
ShoppingCart cart = new ShoppingCart
{
2021-07-05 18:21:52 +10:00
Products = new List<Product>
{
new Product {Name = "Kayak", Price = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
}
};
// get total value of the products in cart
decimal cartTotal = cart.TotalPrices();
Console.WriteLine("Total: {0:c}", cartTotal);
}
}
2021-07-05 18:21:52 +10:00
public static class MyExtensionMethod
{
2021-07-05 18:21:52 +10:00
public static decimal TotalPrices(this ShoppingCart cartParam)
{
2021-07-05 18:21:52 +10:00
decimal total = 0;
foreach (Product prod in cartParam.Products)
{
total += prod.Price;
}
return total;
}
}
}