Negative tive C # item14: using the constructor chain

Source: Internet
Author: User

Writing constructor is usually a repetitive task. We often need to perform the same initialization operation on the same member variable in different declarative constructor. In order to complete this task, there are usually three methods.

  1. Copy/paste the code. After you write a constructor, paste the code into other constructor functions.
  2. The Helper function is used to place the common logic in a separate method, and then the method is called in each constructor.
  3. The constructor chain is used. In one constructor, another constructor is used.

First, we need to discard the above 1st solutions, because repeated code is a very obvious "Bad taste". For the 2nd solutions, from the perspective of eliminating repeated code, there is no problem, but the efficiency of the generated target code is relatively low. The compiler will add some code on each constructor to execute the function logic we need, it adds a variable initializer for each member variable and calls the constructors of the base class. The 3rd schemes are the simplest and most efficient.

We can use the constructor chain in this way.

Code

  1 public class MyClass
2 {
3 // collection of data
4 private ArrayList _coll;
5 // Name of the instance:
6 private string _name;
7
8 public MyClass() :
9 this( 0, "" )
10 {
11 }
12
13 public MyClass( int initialCount ) :
14 this( initialCount, "" )
15 {
16 }
17
18 public MyClass( int initialCount, string name )
19 {
20 _coll = ( initialCount > 0 ) ?
21 new ArrayList( initialCount ) :
22 new ArrayList();
23 _name = name;
24 }
25 }

The sequence of each step in the C # object initialization process is as follows.

  1. Set all static variables to 0
  2. Execute static variable initialize
  3. Execute the static constructor of the base class
  4. Execute the static constructor of the current type
  5. Set all instance variables to 0
  6. Execute the instance variable Initiator
  7. Execute appropriate base class instance Constructor
  8. Execute the example constructor of the current type

When creating an object, the C # compiler will ensure that all the variables are correctly initialized in some way. At least, we can ensure that all of its memory is set to 0 when the object instance is created. Both static and instance members follow this rule. But our goal is to ensure that all variables are initialized as expected and only once.

We should use the variable initialization tool to initialize simple resources. If complicated logic is needed, we need to use constructors to execute initialization, but at this time we 'd better put some shared logic in some constructors, then, use the constructor to call them to minimize code duplication.

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.