Thursday, October 26, 2006

Generics - What i think it is "Shopping Cart Sample"


So today i begin my road to discovery regarding generics.

This article tells me the following regarding generics..

1. New feature introduced in .NET C# 2.0 and CLR 2.0.
2. It gives classes and methods the ability to defer the specification of one or more types until the class or method is-declared and instantiated by client code.

The following sample shows an example of generics ("in less than greater than signs")


namespace SampleShoppingApplication
{
---[Serializable]
---public class ShoppingCart
---{
------private List<item> _items = new List<item>();
------public Collection<item> Items {
---------get { return new Collection<item>(_items); }
------}
------public float TotalCost {
---------get {
------------float sum = 0F;
------------foreach (Item i in _items) sum += i.Cost;
------------return sum;
---------}
------}
---}


---[Serializable]
---public class Item {
------private string _description;
------private float _cost;
------public Item() : this("", 0F) { }
------public Item(string description, float cost) {
---------_description = description;
---------_cost = cost;
------}
------public string Description {
---------get { return _description; }
---------set { _description = value; }
------}
------public float Cost {
---------get { return _cost; }
---------set { _cost = value; }
------}
---}
}