this is an article on constructors in c#, for the beginner level programmers. it covers simple constructors, constructors overloading, behaviour of constructors in inheritance, constructor chaining and static constructors. at the end, it contains the general faqs about constructors.
introduction 【相关文章:基于LmNet PF快速构建神经网络应用】
【扩展阅读:谁谋杀了Wrox】
public class mysampleclass { public mysampleclass() { // this is the constructor method. } // rest of the class members goes here. }when the object of this class is instantiated, this constructor will be executed. something like this: 【扩展信息:一个智能指针的实现】
broadly speaking, a constructor is a method in the class which gets executed when its object is created. usually, we put the initialization code in the constructor. writing a constructor in the class is damn simple, have a look at the following sample:
mysampleclass obj = new mysampleclass() // at this time the code in the constructor will // be executedconstructor overloading
c# supports overloading of constructors, that means, we can have constructors with different sets of parameters. so, our class can be like this:
public class mysampleclass { public mysampleclass() { // this is the no parameter constructor method. // first constructor } public mysampleclass(int age) { // this is the constructor with one parameter. // second constructor } public mysampleclass(int age, string name) { // this is the constructor with two parameters. // third constructor } // rest of the class members goes here. }well, note here that call to the constructor now depends on the way you instantiate the object. for example:
... 下一页