I don't think you can do this (and I think you don't want to do this). C# might allow it maybe, but it is kind of nonsense to do (and FxCop surely will complain too).

What is the real/actual scenario, or are you just curious?

By on 6/9/2010 9:47 AM ()

Thanks for reply. Yes C# allows that. The example could be a class which doing some initialization in base constructor and calls some virtual Init method. Something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
type ClassA = class 


    abstract member Init : unit -> unit

    default this.Init() = 

        // some initialization

        ()


    new () as this = {} then

        this.Init()


end

type ClassB() = class 

    inherit ClassA()


    override this.Init() =

        base.Init()

        // some initialization

        ()


end

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    public class ClassA

    {

        protected virtual void Init()

        {

        }


        public ClassA()

        {

            Init();

        }

    }


    public class ClassB : ClassA

    {

        protected override void Init()

        {

            base.Init();

        }

    }
By on 6/9/2010 10:16 AM ()

Get rid of the virtual Init method, and put the code in the base and derived constructors. Calling virtual methods from constructors is a dubious feature at best. Assuredly, the base class constructor ought not depend on derived initialization happening before some of its own.

By on 6/9/2010 11:56 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper