Generic type

Classes and structs can be defined with one or more type parameters.

Client code supplies the type when it creates an instance of the type.

For example, the List<T> class in the System.Collections.Generic namespace is defined with one type parameter.

Client code creates an instance of a List<string> or List<int> to specify the type that the list will hold.

Generic type is a type with type parameter.

The type parameter is only a place holder.

The following code defineds a generic Stack:

 
using System;
public class Stack<T>
{
    private int position;
    T[] data = new T[100];
    public void Push(T obj) { 
        data[position++] = obj; 
    }
    public T Pop() { 
        return data[--position]; 
    }
}

class Test
{
    static void Main()
    {
        Stack<int> stack = new Stack<int>();
        stack.Push(5);
        stack.Push(10);
        int x = stack.Pop();  
        int y = stack.Pop();  
    }
}
  

A bag is a data structure that can contain zero or more items, including duplicates of items.

 
using System;
using System.Collections.Generic;

public class Bag<T>
{
    private List<T> items = new List<T>();

    public void Add(T item)
    {
        items.Add(item);
    }

    public T Remove()
    {
        T item = default(T);

        if (items.Count != 0)
        {
            item = items[0];
            items.RemoveAt(0);
        }
        return item;
    }
    // A method to provide an enumerator from the underlying list 
    public IEnumerator<T> GetEnumerator()
    {
        return items.GetEnumerator();
    }

    // A method to remove all items from the bag and return them 
    // as an array 
    public T[] RemoveAll()
    {
        T[] i = items.ToArray();
        items.Clear();
        return i;
    }
}

public class MainClass
{
    public static void Main(string[] args)
    {
        // Create a new bag of strings. 
        Bag<string> bag = new Bag<string>();

        bag.Add("D");
        bag.Add("B");
        bag.Add("G");
        bag.Add("M");
        bag.Add("N");
        bag.Add("I");

        foreach (string elem in bag)
        {
            Console.WriteLine("Element: {0}", elem);
        }

        // Take four strings from the bag and display. 
        Console.WriteLine("Removing individual elements");
        Console.WriteLine("Removing = {0}", bag.Remove());
        Console.WriteLine("Removing = {0}", bag.Remove());

        Console.WriteLine("Bag contents are:");
        foreach (string elem in bag)
        {
            Console.WriteLine("Element: {0}", elem);
        }

        // Remove the remaining items from the bag. 
        Console.WriteLine("Removing all elements");
        string[] s = bag.RemoveAll();

        Console.WriteLine("Bag contents are:");
        foreach (string elem in bag)
        {
            Console.WriteLine("Element: {0}", elem);
        }

    }
}
  

The output:


Element: D
Element: B
Element: G
Element: M
Element: N
Element: I
Removing individual elements
Removing = D
Removing = B
Bag contents are:
Element: G
Element: M
Element: N
Element: I
Removing all elements
Bag contents are:
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.