Effective C # uses a member initializer instead of an assignment statement _ base Application

Source: Internet
Author: User
Tags constructor exception handling int size
In general, a class will have multiple constructors. Over time, member variables and constructors are increasing. The easiest way to handle this situation is to initialize the variable when declaring it instead of in each constructor. Whether a class member (static variable) is suitable for instance variables, we should take full advantage of the initialization syntax.

C # programming is, generally when declaring a variable we will initialize it:
Copy Code code as follows:

1 class Employee
2 {
3 private list<employee> emplist = new list<employee> ();
4}

No matter how many constructors we add for the employee class, the emplist variable can be properly initialized because:

The compiler generates code at the very beginning of all constructors, including the default constructor, to define the initializer for the instance member variable (initialization); So we don't need to add initialization code for each of the defined member variables in the constructor--just initialize the declaration.

The initializer can be viewed as another representation of the initialization statement in the constructor. The code generated by the initializer is inserted before the constructor code to execute. The initializer executes before calling the base class constructor for the type, in the same order as the class member variable declaration.

The initialization syntax for C # is one of the simplest ways to avoid the existence of uninitialized variables in a type. However, you should avoid using initializers in the following three scenarios:

1. When initializing an object of 0 or null
Because the default initialization of the system will set everything to 0 or null (value type and reference type) before all code executes. And this step is at the very bottom of the implementation, we can also directly set the object assignment to 0 or null, but obviously this is redundant.

2. Perform different initialization methods on the same variable
One prerequisite for using the initialization statement is that all constructors will set the same value for the variable. Let's look at the following sample code:
Copy Code code as follows:

Class Employee
{
To declare a variable while initializing it.
Private list<employee> emplist = new list<employee> ();

Public Employee ()
{
}

public Employee (int size)
{
Emplist = new list<employee> (size);
}
}

In the code above, when we call the second constructor to create a generic collection that initializes the specified size, two list<employee> are actually created. The first creation immediately becomes garbage-this is because the initializer will execute before all constructors. The code generated by the compiler is similar to the following code:
Copy Code code as follows:

Class Employee
{
declaring variables
Private list<employee> emplist;

Public Employee ()
{
Emplist = new list<employee> ();
}

public Employee (int size)
{
Emplist = new list<employee> ();
Emplist = new list<employee> (size);
}
}

We can see that this will affect the efficiency of the program, created unnecessary objects, so if you need to perform different initialization in different constructors, the correct approach would be to not apply the initializer, but to declare the variable first, and then initialize the member variable in the constructor, as follows:
Copy Code code as follows:

Class Employee
{
declaring variables
Private list<employee> emplist;

Public Employee ()
{
Emplist = new list<employee> ();
}

public Employee (int size)
{
Emplist = new list<employee> (size);
}
}

3. Exception handling is required
The initializer could not be wrapped by a try statement. So exceptions that occur during the execution of an object initializer are passed outside the object. If an exception can be thrown when the object is initialized, we should put this part of the code in the constructor and handle it abnormally. This allows you to implement the necessary recoverability code to create a type instance and handle the exception in a more user-friendly manner.

section:
A member initializer is the simplest way to guarantee that a member variable is initialized in a type--it is initialized when a variable is declared, regardless of which constructor is invoked, and the initializer will be executed before all constructors. This syntax also avoids the omission of important initialization code when adding a new constructor. So, if the initialization value for a member variable of all constructors is the same, then you should use the initializer syntax as much as possible.
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.