Is it possible to implement template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • witnes
    New Member
    • Jul 2006
    • 4

    Is it possible to implement template

    Hello. I want to have something like this:

    CollectionCIass <int> numbers = new CollectionCIass <int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    but I don't know how to implement this collection.

    I can do either like this:
    internal class CollectionCIass : System.Collecti ons.Generic.ILi st<int>

    but then I can't declare variable like CollectionCIass <int> numbers = new CollectionCIass <int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    I need to declare it like
    CollectionCIass numbers = new CollectionCIass { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    but I want to specify type in parenthesis.
  • witnes
    New Member
    • Jul 2006
    • 4

    #2
    After some investigations I've found answer:

    public class CollectionCIass <T> : System.Collecti ons.IEnumerable
    {
    private List<T> listOfData = new List<T>();
    public void Add(T item)
    {
    listOfData.Add( item);
    }

    #region IEnumerable Members

    public System.Collecti ons.IEnumerator GetEnumerator()
    {
    foreach (T data in listOfData)
    {
    yield return data;
    }
    }

    #endregion
    }

    Comment

    Working...