Just try effectivecsharp_7

Source: Internet
Author: User

Item 14: minimize duplicate initialization logic minimizes repeated initialization Logic

  • The writer constructor is often a type of repetitive work. Many people write the first constructor and copy and paste it to another constructor to meet the heavy load of the class. From now on, do not do this. When you find that multiple constructors have the same logic, extract the same logic and put it in a public constructor. You will experience the PreventionCodeThe benefit of repetition. The compiler also minimizes the Execution Code. The constructor allows one constructor to access another constructor.

        Public     Class  Myclass
    {
    // Collection of data
    Private List < Importantdata > Coll;
    // Name of the instance:
    Private String Name;
    Public Myclass ():
    This (0,"" )
    {
    }
    Public Myclass ( Int Initialcount ):
    This (Initialcount,String . Empty)
    {
    }
    Public Myclass ( Int Initialcount, String Name)
    {
    Coll = (Initialcount > 0 ) ?
    New List < Importantdata > (Initialcount ):
    New List < Importantdata > ();
    This . Name = Name;
    }
    }

     

  • C #4.0 adds the default parameter so that you can minimize repeated code in the constructor.. C #4.0 adds default parameters, which you can use to minimize the duplicated
    Code in constructors. You cocould replace all the different constructors for myclass with one constructor that specifies default values for all or values of the values:

        Public     Class  Myclass
    {
    // Collection of data
    Private List < Importantdata > Coll;
    // Name of the instance:
    Private String Name;
    // Needed to satisfy the new () constraint.
    Public Myclass ():
    This (0,String . Empty)
    {
    }
    Public Myclass( IntInitialcount= 0,StringName= "" )
    {
    Coll = (Initialcount > 0 ) ?
    New List < Importantdata > (Initialcount ):
    New List < Importantdata > ();
    This . Name = Name;
    }
    }

     We need to make a trade-off (see item 10) when selecting the default parameter or reload ).There are tradeoffs in choosing default parameters over using multiple overloads.The default parameters allow you to make more choices. The above myclass specifies the default value for both parameters. You can specify different values for the parameter (either or both. If you consider all the situations, the reload method requires four different constructors. In addition, as the number of parameters increases, the number of reloads also increases. This complexity also gives default parameters a better way to minimize the number of potential overloading.

  • Defining the default value for all types of constructors means that clear code is required when new myclass () is called.Defining default values for all parameters to your type's constructor means that user code will be valid when you call the new myclass(). When you want to use this method, you need to create a constructor without parameters (as in the preceding example ). Although most codes can use default parameters, classes that use the new () method generally do not accept them as default parameter constructors. Therefore, a class must have a constructor without parameters.

  • You may find that in the second constructor, specify "" as the default value for the name parameter, rather than the common string. Empty. This is because string. Empty is not a "compile-time" constant. It is a static attribute in the string class.

  • No default parameters are supported from C # versions 1.0 to 3.5. At that time, you will use one constructor to call the function Chain Method of another constructor, instead of creating a public method.

        Public     Class  Myclass
    {
    // Collection of data
    Private List < Importantdata > Coll;
    // Name of the instance:
    Private String Name;
    Public Myclass ()
    {
    Commonconstructor ( 0 , "" );
    }
    Public Myclass ( Int Initialcount)
    {
    Commonconstructor (initialcount, "" );
    }
    Public Myclass ( Int Initialcount, String Name)
    {
    Commonconstructor (initialcount, name );
    }
    Private Void Commonconstructor ( Int Count, String Name)
    {
    Coll = (Count > 0 ) ?
    New List < Importantdata > (Count ):
    New List < Importantdata > ();
    This . Name = Name;
    }
    }

    This version looks similar, but it is inefficient. The pace of the compiler extracts repeated code from the public methods you write.

  • Both default functions and reloads have their own usage. In short, you should try to use the default parameters instead of the constructor overload.Both default parameters and overloads have their place. In general, you shocould prefer default values to overloaded constructors.

  • This is the last item about object initialization in C #. Here is the order of operations for constructing the first instance of a type:

        1  . Static variable Storage  Is     Set  To  0  .
    2 . Static variable initializers execute.
    3 . Static Constructors For The Base Class Execute.
    4 . Static Constructor executes.
    5 . Instance variable Storage Is Set To 0 .
    6 . Instance variable initializers execute.
    7 . The appropriate Base Class Instance constructor executes.
    8 . The instance constructor executes.

     

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.