我有以下代码:
public AccountService(ModelStateDictionary modelStateDictionary, string dataSourceID)
{
this._modelState = modelStateDictionary;
this._accountRepository = StorageHelper.GetTable<Account>(dataSourceID);
this._productRepository = StorageHelper.GetTable<Product>(dataSourceID);
}
public AccountService(string dataSourceID)
{
this._accountRepository = StorageHelper.GetTable<Account>(dataSourceID);
this._productRepository = StorageHelper.GetTable<Product>(dataSourceID);
}
有什么方法可以简化构造函数,使每个构造函数都不必执行StorageHelper调用?
我还需要指定此内容。 ?
最佳答案
public AccountService(ModelStateDictionary modelStateDictionary, string dataSourceID)
: this(dataSourceID)
{
this._modelState = modelStateDictionary;
}
这将首先调用您的其他构造函数。您也可以使用
base(...
调用基本构造函数。在这种情况下隐含了
this
。关于c# - 我可以在C#中组合构造函数吗,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8461856/