日志文章

2007年11月24日 11:35:07

泛型,索引实例

Copy code
class SampleCollection<T>
{
private T[] arr = new T[100];
private T[,] arr2 = new T[10, 10];
public T this[int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
public T this[int i, int j]
{
get
{
return arr2[i, j];
}
set
{
arr2[i, j] = value;
}
}
}
// This class shows how client code uses the indexer
class Program
{
static void Main(string[] args)
{
SampleCollection<string> stringCollection = new SampleCollection<string>();
stringCollection[0] = "Hello, World";
stringCollection[0, 0] = "i,j";
System.Console.WriteLine(stringCollection[0]);
System.Console.WriteLine(stringCollection[0,0]);
Console.ReadLine();
}
}




//泛型的例子
//class Program
//{
//   static void Main(string[] args)
//   {
//     //产生一个customer的强类型集合
//     List1<Customer> customers = new List1<Customer>();
//     customers.Add(new Customer("Motown-Jobs"));
//     customers.Add(new Customer("Fatman's"));

//     foreach (Customer c in customers)
//         Console.WriteLine(c.CustomerName);
//     Console.ReadLine();
//   }
//}
//public class Customer
//{
//   private string customerName = "";
//   public string CustomerName
//   {
//     get { return customerName; }
//     set { customerName = value; }
//   }
//   public Customer(string customerName)
//   {
//     this.customerName = customerName;
//   }
//}

// 自定的泛型类:
//public class List1<T> : CollectionBase
//{
//   public List() { }

//   public T this[int index]
//   {
//     get { return (T)List[index]; }
//     set { List[index] = value; }
//   }

//   public int Add(T value)
//   {
//     return List.Add(value);
//   }

//}



Tags: index   索引   泛型  

类别: C# |  评论(0) |  浏览(1111) |  收藏
发表评论
看不清楚,换一张