所有的,我有一個應用程序需要定期連接和分離多個SQL數據庫。我想創建一個類,將所有這些數據庫保存爲可迭代的集合。要做到這一點,我從ICollection
繼承,但有一些thinkg我不理解:.NET ICollection類基礎知識
class SqlDataBases : ICollection<SqlDb>
{
private List<SqlDb> dbColl;
public SqlDataBases()
{
dbColl = new List<SqlDb>();
}
// Add an index to the collection.
public SqlDb this[int _nIndex]
{
get { return (SqlDb)dbColl[_nIndex]; }
set { dbColl[_nIndex] = value; }
}
// et al.
}
public class DbEnumerator : IEnumerator<SqlDb>
{
// ...
}
class SqlDb
{
private string strMdfFullPath;
private string strLdfFullPath;
private bool bIsAttached;
public SqlDb(string _strMdfFullPath, string _strLdfFullPath, bool _bIsAttached)
{
this.strMdfFullPath = _strMdfFullPath;
this.strLdfFullPath = _strLdfFullPath;
this.bIsAttached = _bIsAttached;
}
}
我的問題是:「爲什麼從ICollection
繼承可言,當你必須添加方法,如‘添加’,「包含'etc.你自己嗎?或者你必須自己按照MSDN中的建議自己去做?我一直在閱讀「C#in a Nutshell」,這個問題在這本書中很突出。我知道我在這裏失去了一些東西...
我的想法是一樣的;爲什麼不在需要的地方使用'List'?它具有您嘗試實現的功能,只要我能看到。 –
2012-03-01 12:10:51
@Killercam,下面的一條評論建議使用Dictionary <>。除非你想通過名稱反覆「查找」你的連接,否則Dictionary <>會更好,因爲你可以指定一個類似於數據庫名稱的鍵。 – 2012-03-01 12:33:25
感謝您的幫助。 – MoonKnight 2012-03-01 13:36:44