C # Proverbs: Using constructors to initialize statements

Source: Internet
Author: User
Tags comparison comparison table constant constructor copy functions string
function | When writing a program, there will often be a constructor that provides a different scene for a type, perhaps most constructors are similar, and the default parameters are not allowed in C #, so the writing of the constructor is a repetitive task. But using copy and paste to complete the writing of constructors is sometimes difficult to achieve, especially when members are changed. Therefore, a more common substitution method is to provide a unified member function for the constructor to complete the initialization work.

For example:

public class MyClass
{
private int _age;
private string _name;

Public MyClass (String Name)
{
Initmember (Name, 20);
}
Public MyClass (String Name, int age)
{
Initmember (Name, age);
}

<summary>
Init Class Members
</summary>
<param name= "Name" ></param>
<param name= "Age" ></param>

private void Initmember (string Name, int age)
{
_age = age;
_name = Name;
}
}
However, a more concise approach in C # is to invoke another constructor in a constructor initialization statement, and the form above can be rewritten as follows.

public class MyClass
{
private int _age;
private string _name;

Public MyClass (string name): This (name, 20)
{}

Public MyClass (String Name, int age)
{
_age = age;
_name = Name;
}
}
For both, the execution efficiency is not much different, but the latter is much better in terms of code quality and makes the code look clearer.

Second, for the invocation of the base class. Because a member function cannot call the constructor of a base class, it is more difficult to write the first one, which is much simpler.

For example:

public class Myclass:baseclass
{
private int _age;
private string _name;

Public MyClass (string name): This (name, 20)
{}

Public MyClass (string name, int age): Base (name, age)
{
_age = age;
_name = Name;
}
}
One of the things to mention here is the constructor initialization statement, which can only be a single call to "this" or "base", and cannot coexist.

Finally, for the initialization of the ReadOnly constant, the ReadOnly constant cannot be modified in the member function because the ReadOnly constant can be modified only through the member initialization statement or in the constructor, which is also an important factor for constructor initialization statements to be better than member functions.

With the above note, for the comparison of the two, you can form the following simple comparison table.

Efficiency Code structure Base class Call Static member initialization
Constructor Initialization statement The same Concise and clear Easy OK
member functions More clear More Trouble No
(Note: The Copy/paste method is not advocated here to repeat the constructor)

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.