C # Object initialization[full VERSION]

Source: Internet
Author: User
Tags constructor

Original address: http://www.csharp411.com/c-object-initialization/

When constructing a C # object, it is important to understand the scope of the object and the sequence in which the constructor is initialized

Derived static fields

Derived Static constructor

Derived instance Fields

Base static fields

Base Static constructor

Base instance Fields

Base Instance Constructor

Derived Instance Constructor

Example

The following is an example of a C # console program that illustrates the order of object initialization

This program creates the derived object of the base object, contains the static and instantiated constructors and scopes, and the two scopes "Field1" and "Field2" are initialized in their definition, but "Field3" is initialized in the constructor. It also contains a virtual method that proves why a virtual method cannot be invoked from a constructor. The console is written when each scope and constructor is initialized, so you can see the initialized sequence

using System;

namespace Objectinit
{
Class program
{
static void Main (string[] args)
{
Derived d = new Derived ();
Console.ReadLine ();
}
}
Class base
{
Public base ()
{
Console.WriteLine ("Base.Instance.Co Nstructor ");
This.m_field3 = new Tracker ("Base.instance.field3″");
this. Virtual ();     
}
Static Base ()
{
Console.WriteLine ("Base.Static.Constructor");

Private Tracker m_field1 = new Tracker ("Base.instance.field1″");
Private Tracker m_field2 = new Tracker ("Base.instance.field2″");
Private Tracker m_field3;
Static private Tracker s_field1 = new Tracker ("Base.static.field1″");
Static private Tracker s_field2 = new Tracker ("Base.static.field2″");     
Virtual public void virtual ()
{
Console.WriteLine ("Base.Instance.Virtual");
}
}
Class Derived : Base
{
Public Derived ()
{
Console.WriteLine ("Derived.Instance.Constructor");
this.m_field3 = new Tracker ("Derived.instance.field3″");     
}
Static Derived ()
{
Console.WriteLine ("Derived.Static.Constructor");

Private Tracker m_field1 = new Tracker ("Derived.instance.field1″");
Private Tracker m_field2 = new Tracker ("Derived.instance.field2″");
Private Tracker m_field3;
Static private Tracker s_field1 = new Tracker ("Derived.static.field1″");
Static private Tracker s_field2 = new Tracker ("Derived.static.field2″");     
Override public void Virtual ()
{
Console.WriteLine ("Derived.Instance.Virtual");

}
Class Tracker
{
Public Tracker (string text)
{
Console.WriteLine (text);
}
}
}

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.