如何避免代码冗余C#构造函数重载

时间:2019-05-29 17:52:48

标签: c# parameters constructor overloading

这是我在类中使用构造函数重载的方式,但是您可以看到在这两种方法中都已重用了一些代码。避免这种情况的最好方法是什么?根据这种重载方法,如何提高代码的可重用性。

public class Customer{

        public Customer(creditCardType ccType, bool isactive, string connectionString,  string language = "en")
        {
            this.CreditCardType = ccType;
            this.IsActive = isactive;
            CustomerAccountNo cusAccNo = new CustomerAccountNo(connectionString);
            DataTable AccNoTextDataTable = cusAccNo.GetTextInLanguages();
            this.DisplayText = AccNoTextDataTable.AsEnumerable().Where(r => r["LanguageKey"].ToString() == language).FirstOrDefault()["LocalizedCustomText"].ToString();
        }

        public Customer(creditCardType ccType, bool isactive, string language = "en")
        {
            this.CreditCardType = ccType;
            this.IsActive = isactive;
            CustomerAccountNo cusAccNo = new CustomerAccountNo();
            DataTable AccNoTextDataTable = cusAccNo.GetTextInLanguages();
            this.DisplayText = AccNoTextDataTable.AsEnumerable().Where(r => r["LanguageKey"].ToString() == language).FirstOrDefault()["LocalizedCustomText"].ToString();
        }

}

更新: 根据Ed Plunkett的评论,这就是我试图做到的方式,

public Customer(creditCardType ccType, bool isactive, string connectionString, string language = "en"): this(ccType, isactive, language) {
 CustomerAccountNo cusAccNo = new CustomerAccountNo(connectionString);
 DataTable AccNoTextDataTable = cusAccNo.GetTextInLanguages();
}

您能告诉我这是怎么回事

this.CreditCardType = ccType;
this.IsActive = isactive;

上面代码中的部分句柄?

0 个答案:

没有答案