Effective C # Principle 12: Select variable initialization instead of assignment statement

Source: Internet
Author: User
Tags constructor int size

According to my personal understanding of the article, I translate initializer into: initializer, which refers to the initialization syntax, which is the method of creating an instance value directly while declaring a variable in a class.

Example: Object m_o = new Object (), if the code is not in any function, but in a class, it is an initializer, regardless of whether you put it at the beginning of the class or at the end. )

Some classes often have more than one constructor. For a long time, it is rare to synchronize its member variables and constructors. The best way to ensure that such things do not happen is to initialize them directly at the time of the declaration, rather than to assign them within each constructor. And you should use the initializer syntax to initialize both static and instance variables.

In C #, this member variable is constructed naturally when you declare a variable. Direct assignment:

public class MyClass
{
 // declare the collection, and initialize it.
 private ArrayList _coll = new ArrayList( );
}

Ignoring how many constructors you will eventually add to MyClass, _coll will initialize correctly. The compiler produces some code that initializes the instance variables you declare before any one of your constructors is called. When you add a new constructor, the _coll is initialized to you. When you add a new variable, you don't have to add initialization code to all of the constructors, and initialize it directly at the declaration. It is also important that if you do not explicitly declare any of the constructors, the compiler will add one to you by default and add all the variable initialization processes to the constructor.

The initializer is more like a convenient and quick way to the constructor. Initializing the generated code is placed before the type's constructor. Initialization is performed before the constructors of the base class of the type are executed, and they are executed in the order in which you declare them.

Using the initializer is one of the easiest ways to avoid using variables that aren't assigned to you in your type, but that's not very good. In the following three cases, you should not use the initializer syntax. First of all, if you are initializing an object to be 0, or null. The system defaults to 0 for all content before any of your code executes. System 0 initialization is based on the underlying CPU instructions, set for the entire memory block. Any other initialization statements of your 0 are superfluous. The C # compiler faithfully adds additional instructions to set the memory to 0. This is not wrong, but inefficient. In fact, if you're dealing with value-type data, it's not worth it:

MyValType _MyVal1; // initialized to 0
MyValType _MyVal2 = new MyValType(); // also 0

The two statements are to set the variable to 0. The first is to set 0 by setting the memory containing the _MYVAL1, and the second through the IL instruction initobj, which produces boxing and unboxing operations on the variable _MYVAL2. This will take a little extra time (see Principle 17).

The second inefficiency occurs when you add two constructors to an object. You initialize the variables using the initializer, and all constructors initialize the variables. This version of MyClass has two different ArrayList objects within its constructor:

public class MyClass
{
 // declare the collection, and initialize it.
 private ArrayList _coll = new ArrayList( );
 MyClass( )
 {
 }
 MyClass( int size )
 {
  _coll = new ArrayList( size );
 }
}

Related Article

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.