Constructor initialization Statement!!!

Source: Internet
Author: User

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!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.