C# inherit

Case:class A has a construct. class B is inherit from class A and B also has a construct. What's the order of the construct execute?

Result: construct A -> construct B.

Sample:

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 

 6 namespace TestInheritConstruct

 7 {

 8     public class Program

 9     {

10         public static void Main(string[] args)

11         {

12             B b = new B();

13             Console.ReadLine();

14         }

15     }

16 

17     public class A

18     {

19         public A()

20         {

21             Console.WriteLine("Construct A.");

22         }

23     }

24 

25     public class B : A

26     {

27         public B()

28         {

29             Console.WriteLine("Construct B.");

30         }

31     }

32 }
View Code

 

 

你可能感兴趣的:(Inherit)