简体   繁体   中英

C# in VS2005: will this() execute the base constructor code for an inherited class?

For C# in VS2005, will calling this() in an inherited class cause the execution of the base constructor?

EDIT: How can I avoid having to re-write the x and y assignments? Note, I do not want the MyObject(int num) constructor to execute the base() contructor.

public class MyObject : MyParentObject { 

int x; 
int y; 
int z; 

public MyObject() { 
    x = 5; 
    y = 10; 
} 

public MyObject(int num) : base(num) { 
    x = 5; 
    y = 10; 
    z = num; 
} 

base() will be called implicitly by the first constructor to run in the derived class:

public MyObject() {
    x = 5;
    y = 10;
}

public MyObject(int setZ) : this() {
    z = setZ;
}

is equivalent to:

public MyObject() : base() {
    x = 5;
    y = 10;
}

public MyObject(int setZ) : this() {
    z = setZ;
}

The parameterless base constructor will be called implicitly unless you explicitly call a paramterized base constructor.

If you have

class Base { }
class Foo : Base { public Foo() { } }

It is no different from saying

class Foo : Base { public Foo() : base() { } }

So if you have a parameterized constructor for Foo, base() will be called no matter what you do with this() unless you also have a parameterized constructor for Base that you explicitly call.

class Base
{
    public Base() { }
    public Base(int bar) { }
}

class Foo : Base
{
    public Foo() : base() { }
    public Foo(int bar) : base(bar) { }
    // also legal: 
    // public Foo() : base(1) { }
    // public Foo(int bar) : base(1) { }
    // public Foo(int bar) : base() { }
    // public Foo(int bar) { } /* uses implicit call to base() */
    // public Foo() { } /* ditto */
}

Either way, the base class will get instantiated first either through the parameterless constructor (implicitly or explicitly) or through the parameterized constructor (explicitly).

I believe this is the syntax you are looking for:

    class MainClass
{
    MainClass()
    {
        //do something
    }

}

class MyClass : MainClass
{
    MyClass()
        : base()
    {
        // do something else
    }

}

Calling base() will cause it to run the base constructor before the current constructor.

class BaseClass
{
   BaseClass()
   {

   }
}

class MyClass : BaseClass
{
   MyClass(int? id) : base()
   {

   }

   MyClass() : this(null)
   {

   }
}

MyClass() : this(null) will call base via MyClass(int? id)

Or you could swap it around and make MyClass(int? id) : this() and MyClass() : base() either way, the base constructor will be called.

The paramater-less constructor (if there is one) is called if nothing is specified, or a compiler error will result (if you only have a base constructor with parameters)

You're trying to not instantiate your base class at any point? That's not possible; you have to explicitly or implicitly call a base constructor, whether you rewrite the field assigments or not.

It's sounds like this isn't the behavior you're looking for (because it calls base() implicitly, but then again, so does your code), but this saves you the rewrite:

public class MyObject : MyParentObject { 

int x; 
int y; 
int z; 

public MyObject() { 
    x = 5; 
    y = 10; 
} 

public MyObject(int num) : this() { 
    z = num; 
}

Why specifically do you want to avoid calling the base class constructor?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM