傻强

岂是池鱼恋旧渊,敢比雄鹰上九天。
轻酌小唱性情酣,宏图霸业谈笑间。

导航

C#2.0范型---资源库模式

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
for (int i = 0; i < 100; i++)
            
{
                Repository
<Foo>.Instance.Add(new Foo(i));
            }

            Console.WriteLine(Repository
<Foo>.Instance[9].Count);
            Console.ReadLine();
        }

    }


    
public class Repository<T>
    
{
        
private static Repository<T> _instance;
        
private ArrayList _list;

        
public Repository()
        
{
            _list 
= new ArrayList();
        }


        
public static Repository<T> Instance
        
{
            
get
            
{
                
if (_instance == null)
                    _instance 
= new Repository<T>();
                
return _instance;
            }

        }


        
public T this[int index]
        
{
            
get
            
{
                
return (T)_list[index];
            }

        }


        
public int Add(T t)
        
{
            
return _list.Add(t);
        }



    }


    partial 
class Foo
    
{
        
private int _count = 0;
        
public Foo(int count)
        
{
            _count 
= count;
        }


        
public string Count
        
{
            
get
            
{
                
return _count.ToString();
            }

        }

    }

}

posted on 2005-07-06 15:14  傻强  阅读(461)  评论(0)    收藏  举报