今天刚刚开始学习C#泛型,根据网上的教程,写了一个简单的泛型实例:
Stack.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication2
...{
class Stack
...{
private T[] store;
private int size;
public Stack()
...{
store = new T[10];
size = 0;
}
public void Push(T x)
...{
store[size++] = x;
}
public T Pop()
...{
return store[--size];
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication2
...{
class Program
...{
[STAThread]
static void Main(string[] args)
...{
IList
aList.Add(3);
aList.Add(4);
int total = 0;
foreach (int val in aList)
...{
total = total + val;
}
Console.WriteLine("Total is {0}", total);
Stack
stack.Push("ni hao");
stack.Push("wo buhao");
Console.WriteLine(stack.Pop());
Console.WriteLine(stack.Pop());
Console.Read();
}
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2129890