indexer , a very very interesting feature in C# to use object as array.

sampe code – FakeDB.cs
usage:

FakeDB db = FakeDB.GetFakeDB();
DataTable table = new DataTable("project");
/* use table */
db["project"] = table;
 public class FakeDB
    {
        //Global Variable
        private static FakeDB theFakeDB = null;
        //Singleton pattern 
        private FakeDB() {
            Tables = new Dictionary<string, DataTable>();
        }

        //indexer
        public DataTable this[String tname]
        {
            get
            {
                if (this.Tables != null && this.Tables.Keys.Contains(tname))
                    return Tables[tname];
                return null;
            }
            set
            {
                if (this.Tables != null)
                    Tables[tname] = value;
            }
        }

        public static FakeDB GetFakeDB()
        {
            if (theFakeDB == null)
                theFakeDB = new FakeDB();
            return theFakeDB;
        }

        public Dictionary<String, DataTable> Tables;

        public DataTable GetTable(string name) 
        {
            if (Tables.Keys.Contains(name))
                return Tables[name];
            else
                return null;
        }

        public void SetTable(String name, DataTable table)
        {

                Tables[name] = table;
        }
    }
Advertisements