usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacenange_1{classA {ReadOnly intAmyint_1 =3; ReadOnly intAmyint_2 =4; Public stringname; Public intAge ; PrivateA ()//Private constructors perform other constructs{//initialization of a function's common useAmyint_1 =5; Amyint_2=6; Console.WriteLine ("call the parameterless constructor {0} {1}", amyint_1, amyint_2); } PublicAstringStrintK): This() {name=str; Age=K; Console.WriteLine ("call the constructor with a parameter name:{0} {1}", Name,age); } } classProgram {Static voidMain (string[] args) {A OBJB=NewA"Jiang Tao", at); Console.ReadLine (); } }}
/*another case where this syntax is useful is that a class has several constructors, and they all need to execute some common code at the beginning of the process of object construction. In this case, the public code can be extracted as a constructor, and all other constructors are used as constructor initialization statements. Reduced duplication of code!! You might think that you can also declare a method to do these public initializations, and let all the constructors call this method, for various reasons this is not a good idea, first the compiler knows that the method is a constructor and will do some optimizations. Second, sometimes something has to be done in the constructor, but not in some other places like the ReadOnly field! */
Constructor initialization Statement!!!