1. 程式人生 > >C# does not contain a constructor that takes no parameter

C# does not contain a constructor that takes no parameter

take mil base 未定義 有一個 mod 一個 構造函數 bsp

C# 中子類要重用父類的構造函數時, 一般會在子類構造函數後面調用 : base(paratype, para).

如果父類有一個參數個數為1的構造函數, 沒有 0 參構造函數。 子類想要重用這個構造函數, 如果沒有寫 :base(paratype, para), 就會有這個錯誤。

由於假設沒寫, VS 會覺得子類是繼承父類的 0 參構造函數, 可是由於父類並未定義 0 參構造函數。 所以就會報錯。

另外, 能夠在base()中調用一個靜態方法來改動子類構造函數的參數在傳遞給父類構造函數。 如:

class ParentClass
{
     public ParentClass(string Name)
     {}
}
class ChildClass
{
      public ChildClass(string firstName, string familyName):base(CombineName(firstName, familyName)
      {}
       static string ConbineName(string firstName, string familyName)
        {return string.Format("{0},{1}", firstName, familyName);
}


C# does not contain a constructor that takes no parameter