在 C# 中,是否需要调用基构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:00:18  来源:igfitidea点击:

In C#, do you need to call the base constructor?

提问by Guy

In C#, if I have an inherited class with a default constructor, do I have to explicitly call the base class' constructor or will it be implicitly called?

在 C# 中,如果我有一个带有默认构造函数的继承类,我必须显式调用基类的构造函数还是隐式调用它?

class BaseClass
{
    public BaseClass()
    {
        // ... some code
    }
}
class MyClass : BaseClass
{
    public MyClass() // Do I need to put ": base()" here or is it implied?
    {
        // ... some code
    }
}

采纳答案by Ian Nelson

You do not need to explicitly call the base constructor, it will be implicitly called.

您不需要显式调用基构造函数,它将被隐式调用。

Extend your example a little and create a Console Application and you can verify this behaviour for yourself:

稍微扩展您的示例并创建一个控制台应用程序,您可以自己验证此行为:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass foo = new MyClass();

            Console.ReadLine();
        }
    }

    class BaseClass
    {
        public BaseClass()
        {
            Console.WriteLine("BaseClass constructor called.");
        }
    }

    class MyClass : BaseClass
    {
        public MyClass()
        {
            Console.WriteLine("MyClass constructor called.");
        }
    }
}

回答by Lars M?hlum

AFAIK, you only need to call the base constructor if you need to pass down any values to it.

AFAIK,如果您需要将任何值传递给它,您只需要调用基本构造函数。

回答by John Downey

It is implied.

它是隐含的。

回答by Tom Welch

A derived class is built upon the base class. If you think about it, the base object has to be instantiated in memory before the derived class can be appended to it. So the base object will be created on the way to creating the derived object. So no, you do not call the constructor.

派生类建立在基类之上。如果您考虑一下,必须先在内存中实例化基对象,然后才能将派生类附加到它。所以在创建派生对象的过程中会创建基础对象。所以不,你不调用构造函数。

回答by Rob Cooper

It is implied, provided it is parameterless. This is because you need to implement constructors that take values, see the code below for an example:

它是隐含的,前提是它是无参数的。这是因为您需要实现采用 values 的构造函数,请参阅下面的代码示例:

public class SuperClassEmptyCtor
{
    public SuperClassEmptyCtor()
    {
        // Default Ctor
    }
}

public class SubClassA : SuperClassEmptyCtor
{
    // No Ctor's this is fine since we have
    // a default (empty ctor in the base)
}

public class SuperClassCtor
{
    public SuperClassCtor(string value)
    {
        // Default Ctor
    }
}

public class SubClassB : SuperClassCtor
{
    // This fails because we need to satisfy
    // the ctor for the base class.
}

public class SubClassC : SuperClassCtor
{
    public SubClassC(string value) : base(value)
    {
        // make it easy and pipe the params
        // straight to the base!
    }
}

回答by Keith

It's implied for base parameterless constructors, but it is needed for defaults in the current class:

它对于基本无参数构造函数是隐含的,但对于当前类中的默认值是需要的:

public class BaseClass {
    protected string X;

    public BaseClass() {
        this.X = "Foo";
    }
}

public class MyClass : BaseClass
{
    public MyClass() 
        // no ref to base needed
    {
        // initialise stuff
        this.X = "bar";
    }

    public MyClass(int param1, string param2)
        :this() // This is needed to hit the parameterless ..ctor
    {
         // this.X will be "bar"
    }

    public MyClass(string param1, int param2)
        // :base() // can be implied
    {
         // this.X will be "foo"
    }
}

回答by jl23x

You don't need call the base constructor explicitly it will be implicitly called, but sometimes you need pass parameters to the constructor in that case you can do something like:

您不需要显式调用基本构造函数,它将被隐式调用,但有时您需要将参数传递给构造函数,在这种情况下,您可以执行以下操作:

using System;
namespace StackOverflow.Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            NewClass foo = new NewClass("parameter1","parameter2");
            Console.WriteLine(foo.GetUpperParameter());
            Console.ReadKey();
        }
    }

    interface IClass
    {
        string GetUpperParameter();
    }

    class BaseClass : IClass
    {
        private string parameter;
        public BaseClass (string someParameter)
        {
            this.parameter = someParameter;
        }

        public string GetUpperParameter()
        {
            return this.parameter.ToUpper();
        }
    }

    class NewClass : IClass
    {
        private BaseClass internalClass;
        private string newParameter;

        public NewClass (string someParameter, string newParameter)
        {
            this.internalClass = new BaseClass(someParameter);
            this.newParameter = newParameter;
        }

        public string GetUpperParameter()
        {
            return this.internalClass.GetUpperParameter() + this.newParameter.ToUpper();
        }
    }
}

Note: If someone knows a better solution please tells me.

注意:如果有人知道更好的解决方案,请告诉我。